Search Unity

lerp too fast

Discussion in 'Scripting' started by hansd, Aug 2, 2011.

  1. hansd

    hansd

    Joined:
    Nov 15, 2010
    Posts:
    7
    Hello,

    I am using lerp to interpolate between two positions. The interpolation should be finished after 10 seconds, but it reaches the final position after about 1.6 seconds. I can' t figure out, what I am doing wrong.

    Any hints?

    Code (csharp):
    1.  
    2. private var m_currentTime=0.0;
    3. private var m_timeToMove = 10.0;
    4. private var m_newPosition : Vector3;
    5.  
    6. function Start() {
    7.  
    8.     while (m_currentTime < m_timeToMove){
    9.         m_currentTime += Time.deltaTime;
    10.    
    11.         transform.localPosition = Vector3.Lerp (transform.localPosition,m_newPosition, m_currentTime
    12. /m_timeToMove);
    13.    
    14.         print (" pos: " + transform.localPosition + " t: " + m_currentTime/m_timeToMove );
    15.    
    16.         yield;
    17.     }
    18.  
    19.  
    20. }
    21. function SetPosition(l_position){
    22. m_newPosition = l_position;
    23. }
    24.  
     
    Last edited: Aug 2, 2011
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's because you're continuously changing transform.localPosition each frame and using that as the first parameter in Lerp. You'd want to declare a "startPos = transform.position" variable before the loop, and use startPos instead of transform.position in the Lerp. (It's likely you actually meant transform.position rather than transform.localPosition.)

    --Eric
     
  3. hansd

    hansd

    Joined:
    Nov 15, 2010
    Posts:
    7
    Eric

    thanks; with use of startPos, it is running correct.

    I am using localPosition because I would like to change the position relativ to the parent. Should be ok?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, that's fine. I see localPosition used incorrectly sometimes, but not in this case it seems. :)

    --Eric
     
  5. TJaenichen

    TJaenichen

    Joined:
    Oct 12, 2014
    Posts:
    29
    Yup, that was it. Thanks
     
  6. Austin_Hey

    Austin_Hey

    Joined:
    Nov 13, 2016
    Posts:
    4
    This comment helped me in 2019 ! thx :D
     
    techniquea2z likes this.