- Home /
How to Lerp or Slerp Time.timeScale change
Hello, I want to have a smooth transition between,
time.timeScale == 1.0 and time.timeScale == 0.8
How would I go about doing this in JavaScript?
Thanks Daniel
Create a modified deltaTime implementation that is independent of the time scale.
Answer by robertbu · Jul 08, 2013 at 08:08 PM
This is a tricky problem. The issue is that typically 'Time.deltaTime' is use in some form as part of a Lerp(). But here 'deltaTime' will be changing as you change Time.timeScale. One imperfect solution is to grab an arbitrary deltaTime and use that. Sample code:
#pragma strict
private var myDeltaTime : float;
private var speedup = false;
var maxTimeScale = 8.0;
var speed = 4.0;
function Start() {
myDeltaTime = Time.deltaTime;
Debug.Log(myDeltaTime);
}
function Update() {
if (Input.GetKeyDown(KeyCode.A))
speedup = true;
if (speedup)
Time.timeScale = Mathf.MoveTowards(Time.timeScale, maxTimeScale, myDeltaTime * speed);
}
This code speeds up Time.timeScale when the 'A' key is pressed.
hmm.. Seems like there has to be a simpler way? A different function thats more like simple math? Like this: $$anonymous$$athf.Lerp (from : float, to : float, time : float) : float
so
$$anonymous$$athf.Lerp ((Time.timScale == 1.0), (Time.timeScale == 0.8), 2);
I know a fair bit about Unity, and I don't know if a simpler way. If you were working with something besides Time.timeScale, you could do it in one line:
new_val = $$anonymous$$athf.$$anonymous$$oveTowards(cur_val, dest_val, Time.deltaTime * speed);
or
new_val = $$anonymous$$athf.Lerp(cur_val, dest_val, Time.deltaTime * speed);
But since you are changing timeScale, Time.deltaTime is effected impacting the Lerp()/$$anonymous$$oveTowards(). Since you are working with timeScale, you have to jump through some hoops to get it to work.
use Time.realTimeSinceStartup, which is not affected by timeScale.
(myDelta = Time.realTimeSinceStartup - lastTimeIRan);
// use myDelta
lastTimeIRan = Time.realTimeSinceStartup ;
@Louis - nice.
@$$anonymous$$ G - so the code becomes (untested):
private var lastTime : float;
private var speedup = false;
var maxTimeScale = 8.0;
var speed = 4.0;
function Start() {
LastTime = Time.realTimeSinceStartup;
}
function Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
speedup = true;
if (speedup) {
var myDeltaTime = Time.realTimeSinceStartup - lastTime;
lastTime = Time.realTimeSinceStartup;
Time.timeScale = $$anonymous$$athf.$$anonymous$$oveTowards(Time.timeScale, maxTimeScale, myDeltaTime * speed);
}
}
Not less complicated, but more accurate.
@robertbu Yes that seems better, I will test it out i got a massive script made haha ill see on implementing :D! Thanks for the help and ill get back to ya'll tomorrow when i get a chance to try :D
Your answer
![](https://koobas.hobune.stream/wayback/20220613105716im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Smooth the Changes in TimeScale, in TimeBending script 0 Answers
How to lerp an object's scale? 2 Answers
Slerp, lerp or something else? 3 Answers
How to use Lerp? 3 Answers