Move object with ' Lerp() ' problem !

… i’m using the method Vector3.Lerp() in line 16 to move object from position to other position Smoothly, the code working well but the problem:

the object reach new position but not Exact Position, the object stopped before it with little distance !!

void Update ()
{
	Vector3 forward = transform.TransformDirection(Vector3.forward);
	RaycastHit hit;   
	   if(!rayHit)
		{
	   Debug.DrawRay(transform.position, forward * 2, Color.green);
	   transform.rotation = Quaternion.Euler(0.0f, Mathf.PingPong(Time.time * rotateSpeed, -115.0f), 0.0f);
		}
		if(Physics.Raycast(transform.position, forward, out hit, 2))
			{
	      	if(hit.collider.gameObject.name == "player1"  !inside)
				{
				rayHit = true;
		player1.rigidbody.isKinematic = true;
		player1.transform.position = Vector3.Lerp(player1.transform.position, new Vector3(transform.position.x,transform.position.y,transform.position.z), 0.2f);
}

i notice something:
when i put line 16 outside, direct in Update method, the object reach Exact new position !

this is because by nature, linear interpolation never reaches its exact goal - just very close to it smoothly, infinitely slowing to an unnoticeable speed. do something like:

if (myObject.transform.position.x > closeToDestination.x){regular transform, stop lerping, go right to destination.}

im C# beginner, can you explain your words with code

you can use Vector3.Dstance

Vector3 currenrtPos = transform.position;
        Vector3 targetPos = target.position;
        float pearSpeed = Time.deltaTime * speed;
        if(Vector3.Distance(targetPos,currentPos)<=pearSpeed)
        {
            currenrtPos = targetPos;
        }else
            {
            currentPos = Vector3.Lerp(currenrtPos, targetPos,pearSpeed);
            }
        transform.position = currentPos;

or at function

static Vector3 LerpAndStop(Vector3 currentPos, Vector3 targetPos,float t)
    {
        return Vector3.Distance(currentPos,targetPos) <= t ? targetPos : Vector3.Lerp(currentPos,targetPos,t); 
    }

ok…, but console gave me this error:

 error CS0019: Operator `<=' cannot be applied to operands of type `method group' and `float'

The console refers to this line:

if(Vector3.Distance <= pearSpeed)

I did not test the code. Try to replace by Vector3.Distance(targetPos,currentPos)
but batter use the function

static Vector3 LerpAndStop(Vector3 currentPos, Vector3 targetPos,float t)
	{
		return Vector3.Distance(currentPos,targetPos) <= t ? targetPos : Vector3.Lerp(currentPos,targetPos,t); 
	}

You can add it to your class this function and use it as Vector3.Lerp

Because that’s not the line of code he provided.

Really useful! I came aroun this yesterday in my own code also and I didn’t consider the possibility of encapsulating this into a single function.

Well, it’s only doing that because it’s not being used appropriately. As the name suggests the velocity is in fact linear. The result returned for t = 0 is the exact starting position, and the result for t = 1 is the exact end position. So if you use it with that in mind you will in fact get things moving linearly between two exact points.

The reason it works as you describe here is that it is not actually being used as a linear interpolation, because t is not being changed but the starting position is. You’d typically do the opposite. That code simply moves player1 20% closer to the transform each frame.

Woody, the way that a lerp is generally used is something along the lines of the following.

  1. When you start the lerp, store the starting position and some kind of timer.
  2. Each frame, update the timer. Compare the timer to your desired duration to come up with your interpolation factor (a number between 0 and 1 which increases from 0 at the start to 1 upon completion).
  3. Use the above along with your target position in the lerp function.

For bonus points, once you’ve got that working you can swap Lerp out for SmoothStep, which works similarly but eases in and out at the end. (This looks smoother if you’re using it for certain types of animation.)

Alternatively, if you don’t care about the duration and just want one thing to move towards another, you could also try using MoveTowards instead of Lerp (since you are in fact using Lerp as a kind of MoveTowards in this case anyway).

Thanks Angrypenguin, I just learned that I was using Lerp wrong… I found some more info explaining what you mean:

http://answers.unity3d.com/questions/14288/can-someone-explain-how-using-timedeltatime-as-t-i.html

It can be a tricky one to grasp at first if you don’t already know what it’s meant to do, because it does look like it’s doing something sort of right.