Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Move object with ' Lerp() ' problem !

Discussion in 'Scripting' started by woodygoody, Sep 4, 2013.

  1. woodygoody

    woodygoody

    Joined:
    Aug 23, 2011
    Posts:
    164
    .. 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 !!
    Code (csharp):
    1.  
    2. void Update ()
    3. {
    4.     Vector3 forward = transform.TransformDirection(Vector3.forward);
    5.     RaycastHit hit;  
    6.        if(!rayHit)
    7.         {
    8.        Debug.DrawRay(transform.position, forward * 2, Color.green);
    9.        transform.rotation = Quaternion.Euler(0.0f, Mathf.PingPong(Time.time * rotateSpeed, -115.0f), 0.0f);
    10.         }
    11.         if(Physics.Raycast(transform.position, forward, out hit, 2))
    12.             {
    13.             if(hit.collider.gameObject.name == "player1"  !inside)
    14.                 {
    15.                 rayHit = true;
    16.         player1.rigidbody.isKinematic = true;
    17.         player1.transform.position = Vector3.Lerp(player1.transform.position, new Vector3(transform.position.x,transform.position.y,transform.position.z), 0.2f);
    18. }
    19.  
    i notice something:
    when i put line 16 outside, direct in Update method, the object reach Exact new position !
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    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.}
     
  3. woodygoody

    woodygoody

    Joined:
    Aug 23, 2011
    Posts:
    164
    im C# beginner, can you explain your words with code
     
    Last edited: Sep 4, 2013
  4. medvedya

    medvedya

    Joined:
    Mar 19, 2013
    Posts:
    2
    you can use Vector3.Dstance
    Code (csharp):
    1.  
    2. Vector3 currenrtPos = transform.position;
    3.         Vector3 targetPos = target.position;
    4.         float pearSpeed = Time.deltaTime * speed;
    5.         if(Vector3.Distance(targetPos,currentPos)<=pearSpeed)
    6.         {
    7.             currenrtPos = targetPos;
    8.         }else
    9.             {
    10.             currentPos = Vector3.Lerp(currenrtPos, targetPos,pearSpeed);
    11.             }
    12.         transform.position = currentPos;
    13.  
    or at function
    Code (csharp):
    1.  
    2. static Vector3 LerpAndStop(Vector3 currentPos, Vector3 targetPos,float t)
    3.     {
    4.         return Vector3.Distance(currentPos,targetPos) <= t ? targetPos : Vector3.Lerp(currentPos,targetPos,t);
    5.     }
    6.  
     
    Last edited: Sep 4, 2013
  5. woodygoody

    woodygoody

    Joined:
    Aug 23, 2011
    Posts:
    164
    ok.., but console gave me this error:
    Code (csharp):
    1.  error CS0019: Operator `<=' cannot be applied to operands of type `method group' and `float'
    The console refers to this line:
    Code (csharp):
    1. if(Vector3.Distance <= pearSpeed)
     
  6. medvedya

    medvedya

    Joined:
    Mar 19, 2013
    Posts:
    2
    I did not test the code. Try to replace by
    Code (csharp):
    1. Vector3.Distance(targetPos,currentPos)
    but batter use the function
    Code (csharp):
    1.  
    2. static Vector3 LerpAndStop(Vector3 currentPos, Vector3 targetPos,float t)
    3.     {
    4.         return Vector3.Distance(currentPos,targetPos) <= t ? targetPos : Vector3.Lerp(currentPos,targetPos,t);
    5.     }
    You can add it to your class this function and use it as Vector3.Lerp
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Because that's not the line of code he provided.
     
  8. Stilghar

    Stilghar

    Joined:
    Feb 4, 2013
    Posts:
    82
    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.
     
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    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).
     
    Last edited: Sep 9, 2013
  10. Stilghar

    Stilghar

    Joined:
    Feb 4, 2013
    Posts:
    82
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    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.