x


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?

more ▼

asked Apr 07 '10 at 02:58 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Apr 07 '10 at 03:07 PM

duck gravatar image

duck ♦♦
41k 92 148 415

Sigh, again @Duck beats me to the post :) Fortunately I checked before posting mine. Good Answer, though. +1

Apr 07 '10 at 03:10 PM Cyclops

@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.

Apr 07 '10 at 03:11 PM e.bonneville

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.

Apr 07 '10 at 03:13 PM duck ♦♦

@Duck Thanks again... This logic is somewhat hard for me to grasp.

Apr 07 '10 at 03:13 PM e.bonneville

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

Apr 07 '10 at 03:17 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5100
x573
x212
x177
x73

asked: Apr 07 '10 at 02:58 PM

Seen: 2023 times

Last Updated: Apr 07 '10 at 03:11 PM