- Home /
An Explanation of Multiplication (*) and how it relates to Time, deltaTime and framerate independent motion
Why do you use multiplication in scripting, and how does it work? For instance, if you want to change frame-rate to seconds or something, why would you use the * symbol? Like this:
var speed = 5;
function Update () { transform.Translate (0,0,speed * Time.time); }
From what I understand, that changes translate
from frame-rate dependency to being time based, but how does it work? Is the * sign, not, like I think it is, actual multiplication, but a sort of 'translate' function?
Answer by duck · Apr 07, 2010 at 03:07 PM
No, it really is just basic multiplication. I have a feeling that in your example what you really meant to put is Time.deltaTime rather than Time.time.
This is because the value of Time.time increases linearly throughout the course of your playback - counting the seconds elapsed since the playback started, whereas Time.deltaTime gives you a value which is equal to the amount of time elapsed since the last frame.
If your game is running at a steady framerate, the value of Time.deltaTime will maintain a steady value, and it will only increase or decrease in value proportionally to the variations in the framerate that your game is achieving.
So, taking your example (but using deltaTime), and assuming the game is running at, say, around 60 frames per second, the value for deltaTime would be around 0.01666.
This means the object is moved a distance of ( 5 x 0.01666 ) units each frame, and if this happens 60 times a second, it will end up moving 5 units each second.
If the framerate dropped to 20 frames per second, the value of deltaTime would rise to 0.05 (because more time elapses between frames), and you'd end up with the object moving a distance of ( 5 x 0.05 ) units each frame, but because this is now happening only 20 times a second, it still ends up moving 5 units per second.
This is why multiplying your speed by the deltaTime value gives you framerate independent code.
Sigh, again @Duck beats me to the post :) Fortunately I checked before posting $$anonymous$$e. Good Answer, though. +1
@Duck Oh, I see. Thanks a lot. What about other uses for *? Are they all really multiplication? I see it all over the place. For instance, when you use it in addForce. Noob question, I know, but there are just some things I need to know, no matter what the cost.
Yes, with force it is just multiplication too, although often it's multiplying a Vector3, which means you are essentially altering the length of a vector.
@Duck Thanks again... This logic is somewhat hard for me to grasp.
The only time it gets particularly "hairy" is when it comes to multiplying rotations together - then it leaves the realm of normal multiplication and becomes something different. This is why, in the Quaternion documentation, there's a special page on the * operator describing its behaviour! :-) http://unity3d.com/support/documentation/ScriptReference/Quaternion-operator_multiply.html