# how to stop Lerp?? Hot to reach EXACT end position.

I got in trouble with Lerp.
I want to make one gameobject go to another. I am using Vector3.Lerp.

object1.transform.position = Vector3.Lerp (object1.transform.position, object2.transform.position,Time.deltaTime*speed)

object2.transform.position.x = 50, but when it lerps object1 goes straight to this point (x=50),but never reaches it.It stops on 49.9999. I need object1 to reach exactly 50. How to make it ? Maybe there are some ways to stop lerping ?
I was thinking to make like this.

``````object1.transform.position = Vector3.Lerp (object1.transform.position, object2.transform.position,Time.deltaTime*speed)
(STOP LERPING)
object1.transform.position = object2.transform.position
``````

Using Lerp in this way is one of the most widespread anti-patterns in Unity - including in the Unity tutorials themselves

From the manual:

`Lerp(from, to, t)` interpolates between `from` and `to` by the fraction `t`. When `t = 1` returns `to`. When `t = 0.5` returns the point midway between from and to.

So, to actually ever get to the end value, `to`, the third parameter must be `1` (or greater). Your code is passing `Time.deltaTime*speed`. Unless `speed` is greater than your framerate per second (which, from your description, I’m assuming it’s not), this value is always going to be less than 1. So on every frame you move a fraction of the remaining distance closer to `to`, but never quite get there.

You’ll also experience other problems from using this approach - the movement itself will not be linear - the distance covered in each step will decay (fast at first, then slow down as it approaches, which may or may not be what you want).
You’ll also be making your movement framerate dependent, so that the rate of movement varies on different machines, which you almost certainly don’t want.

There’s a good explanation of these problems, and the “correct” way to use Lerp at Using Vector3.Lerp() correctly in Unity — One Man's Trash is Another Man's Blog

You could do that.

Thus your code could look something like

``````private boolean _followObject = true;
private float _minDistance = 0.5;

void Update(){
if(_followObject)
{
if(Vector3.Distance(object1.transform.position, object2.transformation.position >_minDistance)
{
object1.transform.position = Vector3.Lerp(object1.transform.position,object2.transform.position,time.deltaTime*speed);
}
else object1.transform.position = object2.transform.position
}
}
``````

If you use Lerp the way you have then it gets slower and slower as it gets closer, and hence never reaches. It’s one of Zeno’s “paradoxes”.

[Note - technically, since we’re talking about discrete quantities rather than theoretical tortoises, it would get there eventually, in that the distance would get to be less than Mathf.epsilon. But how long this takes depends on the interpolation fraction and it can be a very long time].

The simplest fix is simply to check the distance between the 2 positions after the Lerp call, and if it’s less than some (small) threshold of your choosing, set the moving object’s position to that of the target.

You could also use Lerp the other way, using starting position as the first param and a cumulative time for the interpolation fraction. Then it’d stop because your interpolation fraction would reach 1. But you’d have to find another way of making it slow down as it got closer.

create a static class without mono behavior
then use this function

``````//interpolate an object from A to B over a fixed time
public static IEnumerator Interpolate(Transform obj, Vector3 destination, float overTime)
{
Vector3 source = new Vector3(obj.position.x,obj.position.y,obj.position.z);//deep copy
float startTime = Time.time;
while(Time.time < startTime + overTime && obj != null)
{
obj.position = Vector3.Lerp(source,destination,(Time.time - startTime)/overTime);
yield return null;
}
obj.position = destination;
}
``````

Then from anywhere in any other class:
StartCoroutine(NameOfTheStaticClass.Interpolate(theObjectTransformToMove, vectorEndPos, timeOfTheInterpolation));

and if you start this coroutine from a coroutine, you can wait for it to end like that

yield return StartCoroutine(NameOfTheStaticClass.Interpolate(theObjectTransformToMove, vectorEndPos, timeOfTheInterpolation));

you can modify this function to interpolate colors, rotation and etc…

setting right position at and not is really wrong, however maybe that there is only a precision problem ( float error )

Try:

``````vector3 startPoint = object1.transform.position;
float t=0;

while(t<=1)
{
t += Time.deltaTime*speed;
object1.transform.position = Vector3.Lerp (startpoint, object2.transform.position,t);
}
``````

And then, if position isn’t always correct:

object1.transform.position = object2.transform.position;

if((object1.transform.position - object2.transform.position) < 0.5)
isLerping = false;
object1.transform.position = object2.transform.position

You can edit “0.5” part to make it useful for you program. Good luck.