- Home /
How do I reset a lerp when restarting my game?
I have a value counting up from 12 to 200 using a lerp like this:
globalSpeed = Mathf.Lerp(12, 120, Time.time * 0.0015f);
When restarting the game with:
Application.LoadLevel();
The globalSpeed variable continues to count from where it stopped last time, how can I fix this?
Time.Time is your problem here, its the time since the game started. So Application.LoadLevel() will not "Restart" the level and Time.time will continue to count on.
Answer by Neamtzu · Mar 19, 2015 at 04:17 PM
The easiest way is to have a float variable that is made 0 in Start. In Update just make yourVariable += Time.deltatime;
Use yourVariable instead of Time.time in your lerp.
Another solution will be to use a float variable to cache the Time.time in Start and then your lerp will became globalSpeed = Mathf.Lerp(12, 120, (Time.time - yourVariable) * 0.0015f);
Answer by fffMalzbier · Mar 19, 2015 at 03:56 PM
How about using Time.timeSinceLevelLoad instead of Time.time. That should solve the problem.
Your answer

Follow this Question
Related Questions
How to move an object from point A to point B with one key press 3 Answers
Grid Movement - Lerp that ends with precise values 6 Answers
Lerping textures 3 Answers
Animate buttons on click. 2 Answers
Move Transform to Target in X seconds 3 Answers