Question

What are other scripts to make an object move to a goal? Right now I'm using the Vector3.Lerp method. This works, but the moving gameobject slows down the closer it gets to its goal. I don't wont this to happen. It has to move straight to the goal without slowing down.

Help is appreciated.

Was it helpful?

Solution

One approach is just to work out the direction it should head in and then move at a constant speed in that direction:

function Update() {
    var dir = target - transform.position;
    dir.Normalize();
    transform.position += dir * speed * Time.deltaTime;
}

You'd have to make it a little more complicated than that so that it doesn't overshoot on the final frame, but this should give you a crude approximation of what you want.

OTHER TIPS

You can use this function:

Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);

It should do exactly what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top