# Vector3.Lerp - Constant speed between distance changes

I’m lerping from object 1 to object 2. I want the rate at which object 1 travels to be the exact same, using Time.deltaTime or Time.time. So if the distance between object 1 and object 2 are 5 meters at one point, I want object 1 to travel at 1 meters per second. If they were 1,000 meters apart, I still want object 1 to travel 1 meter per second. I know the solution is right at my fingertips, but I can’t figure it out. I know it’s gotta do with Vector3.Distance(object1.transform.position, object2.transform.position) but I don’t know what else to do!

If I have speed set defaultly at 2, what can I do to make sure that the `Vector3.Lerp (obj1, obj2, Time.deltaTime * newSpeed)` that the `newSpeed` is the correct speed (it’s gotta be less than the original speed, which is 2 in this case)? Thanks.

1 Like

A solution is to use Vector3.MoveTowards() rather than Vector3.Lerp().

``````transform.position = Vector3.MoveTowards(transform.position, other.position, Time.deltaTime * speed);
``````

Speed is a variable you define and is measured in units per second. So if you set it to 1.0, your object will move at a constant 1.0 units per second no matter the distance. Note that in this form of MoveTowards, the first parameter must be the current position of the object, not the starting position of the object.

Vector3.MoveTowards() is likely the best approach, but here’s the answer for Lerp:

``````        float distance = Vector3.Distance(object1.transform.position, object2.transform.position);
float finalSpeed = (distance / newSpeed);
transform.position = Vector3.Lerp(obj1, obj2, Time.deltaTime / finalSpeed);
``````

Dividing Time.deltaTime by distance makes it move as you wanted.

Dividing distance by newSpeed changes the speed.

Now just find the best value for newSpeed (might need to increase it a little).

Thanks, I use both:

transform.position = Vector3.MoveTowards (transform.position, other.position, Time.deltaTime * speed / 2);

transform.position = Vector3.Lerp (transform.position, other.position, Time.deltaTime * speed / 2);

to balance the Lerp acceleration and linear effects. Is there a better way of doing this?