Why is my object not following my path ?,Why is my object going through the target ?

I want to make my object follow a simple path. Just going fowards for a bit then turning 90° left, going foward for a bit and stopping.
I’ve made a script for my object to follow waypoint. The problem is that my object is just going straight fowards indefinitely and not turning or stopping at my target.

Some help would be appreciated

Here’s the script

public Transform[] target;
public float speed;

private int current;


void Update () {
    if (transform.position != target[current].position)
    {
        Vector3 pos = Vector3.MoveTowards(transform.position, target[current].position, speed * Time.deltaTime);
        GetComponent<Rigidbody>().MovePosition(pos);
    }
    else current = (current + 1) % target.Length;

}

Good day.

First: Do you know about nav mesh agents?

Second: I dont know how your objects are, but this " if (transform.position != target[current].position) " is difficult that they have exatcle the same position if they have colliders and rigidbody… You need to chek the distance betwen both objects, and if is smaller than some value, execute it. Something like this:

if (Vector3.Distance (transform.position, target[current].position) > 5)

So it will be true when the distance is less than 5 units.

Bye

hi,
You can use bezeir curve or some waypoints system for moving an object on particular path.
it will work.

Here is an example, i am using waypointCircuit script for defining path. and here how it is working.

 public WaypointCircuit path;
 private void FixedUpdate()
        {
               CheckWaypointDistance();
          }

 void CheckWaypointDistance()
        {
            deltaTime += (Time.deltaTime * bikeSpeed*2);
            nextPoint = path1.GetRoutePoint(deltaTime).position;
 transform.position =nextPoint ;
}