Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at during this transition time.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.

    Read our full announcement for more information and let us know if you have any questions.

Hoping for a explaination about lerp over time...

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

  1. Alima


    Nov 13, 2009
    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;
    6.         var newSpeed:float = Mathf.Lerp(currSpeed, topSpeed, accelPrecent);
    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.

  2. Eric5h5


    Volunteer Moderator Moderator

    Jul 19, 2006
    This might help some.

  3. Vimalakirti


    Oct 12, 2009
    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;
    5. function Accelerate(){
    6.    if (speed < maxSpeed){
    7.        speed = speed + (Time.deltaTime * accelRate);  
    8.    }
    9. }

    Nov 26, 2010
    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
    4. void Start(){
    5.     startTime = Time.realtimeSinceStartup;
    6. }
    8. void Update(){
    9.      interpolatedValue = Mathf.Lerp(startingValue, endingValue, (Time.realtimeSinceStartup - startTime) / totalDuration);
    10. }