- Home /
What is the reasoning of multiplying by Time.deltaTime
More and more I research unity code, I find people multiplying by Time.deltaTime for moving object positions and scaling objects.
So in essence, what would the difference be between these two snippets of code.
What are the implications of not using time.deltatime and using?
// With time.deltaTime
transform.position = Vector3.Lerp(transform.position, target, smoothing * Time.deltaTime);
transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
// Without time.deltaTime
transform.position = Vector3.Lerp(transform.position, target, smoothing);
transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed);
Answer by SohailBukhari · Jul 26, 2017 at 09:31 AM
When you multiply with Time.deltaTime you essentially express: I want to move this object speed
meters per second.So, your code is not dependent to frame rates of your game.
transform.position = Vector3.Lerp(transform.position, target, smoothing * Time.deltaTime);
transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
In this snippet your interpolation is frame rate dependent if your game fps low due to some reason he this will behave odd.So, you are moving speed
meters per frame.
Lets suppose you have speed 10 then your are moving(lerping) 10 meters per frame instead of 10 meters per second.
transform.position = Vector3.Lerp(transform.position, target, smoothing);
transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed);
See the Docs : https://docs.unity3d.com/ScriptReference/Time-deltaTime.html
Thanks for the explanation, so basically, unless you use the time.timedelta mutliplication, you are setting yourself up for unexpected problems?
The specific problem if you don't multiply by deltaTime is "If you play your game on a faster computer, everything will move faster (your character, enemies etc.)", because it is processing more frames per second. This is usually undesirable.
We are making movements smooth not for unexpected problems so that not looks leggy when play game on faster devices.