this script i attach to a collider and it pulls objects accross the colliders axis i chose. so if i set it to x axis any object that enters world travel along the colliders x axis at a speed i chose. but i want any object that enters just to move across the collider without following an axis. how can i do this? here is the script i attach to the colliders. @Priyanka-Rajwanshi @Bunny83 @Kilsnus @davidcox70 @oroora6_unity
@tedesignz1
private List<GameObject> pullObjects;
public Vector3 pullDirection;
public float pullSpeed;
void Start () {
pullObjects = new List<GameObject> ();
}
void Update () {
foreach (GameObject obj in pullObjects) {
obj.transform.Translate (Time.deltaTime * pullSpeed * pullDirection,transform);
}
}
public void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.gameObject.tag == "Untagged") {
pullObjects.Add (coll.gameObject);
}
}
public void OnTriggerExit(Collider col)
{
if (col.gameObject.gameObject.tag == "Untagged") {
pullObjects.Remove (col.gameObject);
}
}
}
so i had an idea on how to fix it but not sure how to apply it to the script. i was thinking to have the script make game objects move twards the center of the collider but instead of making them stop once they reach the middle of the collider, continue moving untill they exits the collider. this script pulls objects twards the center of the collider, how can i make it continue moving untill it exit:
private GameObject PullOBJ;
public float PullSpeed;
public float objRotationSpeed;
public float rotation;
public float PullStrength;
public void OnTriggerStay (Collider coll)
{
if (coll.gameObject.tag == "Untagged") {
PullOBJ = coll.gameObject;
PullOBJ.transform.position = Vector3.MoveTowards (PullOBJ.transform.position, this.transform.position, PullSpeed * Time.deltaTime);
PullOBJ.transform.RotateAround (transform.position, Vector3.up, Time.deltaTime * rotation);
PullOBJ.transform.Rotate (Vector3.left, 45 * Time.deltaTime * objRotationSpeed);
}
}
}
Hello,
From your description, you want something like custom gravity (a pull force) that is pointing a certain axis. So better to use physics.
- Create a cube (our test subject)
- Add rigidbody to cube
- Turn off gravity
- Add a little drag (0.3 maybe)
- Move the cube to x: - 30 y: 4 z:0
- Next create an empty object
- Add a sphere collider to that object
- Turn on trigger
- Add the script below
- Adjust the radius: 50, pull direction: 1,0,0, and pull speed:10
- PLAY
- Watch as it move across
The cube will be pulled or pushed along the x axis (since we set pull direction x:1) and continue moving along that axis.
If you move the cube starting position to x: +20, the cube will be pushed.
Once you grasp the concept, play around with the pull script.
Please assign this script to the empty object
using UnityEngine;
public class PullForce : MonoBehaviour
{
public Vector3 pullDirection;
public float pullSpeed;
public float radius;
private SphereCollider _col;
private void Start()
{
_col = GetComponent<SphereCollider>();
}
private void Update()
{
_col.radius = radius;
}
private void OnTriggerStay(Collider other)
{
float _dist = Vector3.Distance(other.transform.position, transform.position);
float _ratio = _dist / radius;
other.attachedRigidbody.AddForce(pullDirection * pullSpeed * _ratio);
}
}
@Jdogmaster
If you want that the pulled object should carry on its motion even when it exists trigger, you would need two scripts. One on the collider that pulls the objects and other on the objects that are pulled.
public class PullCollider : MonoBehaviour
{
public float pullSpeed;
public void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.gameObject.tag == "Untagged")
{
coll.GetComponent<PulledObject>().StartPull(pullSpeed, (transform.position - coll.transform.position).normalized);
}
}
}
public class PulledObject : MonoBehaviour
{
bool startPulling = false;
float pullSpeed;
Vector3 pulledDirection;
// Use this for initialization
public void StartPull(float _pullSpeed, Vector3 _pulledDirection)
{
startPulling = true;
pullSpeed = _pullSpeed;
pulledDirection = _pulledDirection;
}
// Update is called once per frame
void Update()
{
if (startPulling)
{
transform.Translate(Time.deltaTime * pullSpeed * pulledDirection);
}
}
}