Move towards without stopping at the target location

I want to make an object moves to a target , but to keep it moving (in the same direction of course) after it reaches the target.
I tried moveToward but it just stop when it reach the target…

1 Like

You’ll want to store the direction it’s moving, and then use that. Subtract the positions to get the direction. To keep your speed constant, normalize the direction vector (make it have a length of 1).

Let’s say that you are moving towards “target”:

public Transform target;
private Vector3 movementVector = Vector3.zero;
public float moveSpeed = 1f; //units per second

void Start() { // or whatever function triggers the movement
movementVector = (target.position - transform.position).normalized * moveSpeed;
}

void Update() {
transform.position += movementVector * time.deltaTime;
}
7 Likes

Thanks !

You are a legend, been searching for 3 days!

Sorry for the necropost – but how would I do this if I only had the seconds per unit, and not the units per second?

float unitsPerSecond = 1f / secondsPerUnit;

Or you can just divide instead of multiplying.