Search Unity

Hoping for a explaination about lerp over time...

Discussion in 'Scripting' started by Alima, May 30, 2010.

  1. Alima

    Alima

    Joined:
    Nov 13, 2009
    Posts:
    7
    ok, I have looked through about 30 examples, spent the night going back and forth over the documentation and searching the forums and I feel safe to say the time has come for me to simply as the question.

    How the Mathf.Lerp function actually works is just beyond me at the moment. What I am trying to accomplish is relatively simple. I have a current speed, a top speed and an acceleration rate. Lets say my object is not moving, so its current speed is 0, its top speed could be, lets say 60. The acceleration rate is the amount of time in second I would like it to take to go from 0 to topspeed.

    The only way that comes to mind is by having a variable keep track of the time that I start the acceleration, and using that to figure out what time it should reach top speed, like this:
    Code (csharp):
    1.     function Accelerate(currSpeed:float, startTime:float){
    2.         var targetTime = startTime + accelRate;
    3.         var remainingTime = targetTime - Time.time;
    4.         var accelPrecent = (accelRate - remainingTime) / accelRate;
    5.        
    6.         var newSpeed:float = Mathf.Lerp(currSpeed, topSpeed, accelPrecent);
    7.        
    8.         return newSpeed;
    9.     }
    but that just seems like its overly complicated. And not only that but releasing the accelerate button would force the accelerate timer to then reset and take the full accelerate time to then go from current speed to topspeed, rather than from 0 to topspeed. And frankly doesn't work right anyway. It actually reaches its top speed in about half as much time as the accel rate should let it.

    Now I know I should be able to use variables such as Time.deltaTime to my advantage here, but I just cant grasp how I could do so, and when I have gotten lerps to appear to work in the past using Time.deltaTime in calculating the t parameter of lerp like this, it never seems to actually reach the destination value.

    I would greatly appreciate if someone could explain this chronological nightmare to me so I can figure out what I'm doing wrong and be able to actually use this again in the future.



    Thanks,
    Alima
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    This might help some.

    --Eric
     
  3. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    There's a few ways to do this, but I wouldn't use Lerp for this one. I would use Time.deltaTime

    Code (csharp):
    1. var speed = 0.0;
    2. var accelRate = 10; // add 10 per second, or 0-60 in 6 seconds for this example
    3. var maxSpeed = 60.0;
    4.  
    5. function Accelerate(){
    6.    if (speed < maxSpeed){
    7.        speed = speed + (Time.deltaTime * accelRate);  
    8.    }
    9. }
     
  4. mrwelcam@hotmail.com

    mrwelcam@hotmail.com

    Joined:
    Nov 26, 2010
    Posts:
    17
    You could just divide the past time by the total time

    Code (csharp):
    1. float startTime;
    2. float totalDuration = 5.5f; // tween over five and a half seconds
    3.  
    4. void Start(){
    5.     startTime = Time.realtimeSinceStartup;
    6. }
    7.  
    8. void Update(){
    9.      interpolatedValue = Mathf.Lerp(startingValue, endingValue, (Time.realtimeSinceStartup - startTime) / totalDuration);
    10. }