x


Time.deltaTime?

Can somebody explain to me a bit how Time.deltaTime works? Yes, i have seen the scripting reference page. But it doesnt really explain much. Im trying to learn, so can someone please help me out here?

more ▼

asked Aug 04, 2012 at 08:42 PM

DNP gravatar image

DNP
85 70 69 70

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

3 answers: sort voted first

Time.deltaTime is the time passed since last frame. If you want to move something at a constant velocity speed, for instance, multiply speed by Time.deltaTime and you will get exactly the distance moved by the object since last Update (remember: distance = velocity * time):

var speed: float = 10;

function Update(){ // move the object forward at speed units per second: transform.Translate(transform.forward speed Time.deltaTime); } You can use Input.GetAxis("Vertical") to control the movement:

var speed: float = 10;

function Update(){ // move the object forward/backward at speed units per second: transform.Translate(transform.forward speed Input.GetAxis("Vertical") * Time.deltaTime); } The same applies to the "Horizontal" axis, but not to "Mouse X", "Mouse Y" and "Mouse ScrollWheel"! That's because the axes Vertical and Horizontal return constant values in the range -1..1 when you press the buttons WSAD, while Mouse X, Y and ScrollWheel return the mouse/scrollwheel movement since last frame - thus these values already take the time into account.
Another use for Time.deltaTime is to decrement or increment time at constant rates. If you want to decrement a 10 seconds timer, for instance, you could do this:

var timer: float = 10;

function Update(){ timer -= Time.deltaTime; if (timer <= 0) print("10 seconds ellapsed"); } You can use Time.deltaTime inside Update, coroutines or FixedUpdate (Unity returns the time since last FixedUpdate in this case), but you should not use it inside OnGUI because this function is called several times between frames.

more ▼

answered Aug 04, 2012 at 11:55 PM

aldonaletto gravatar image

aldonaletto
57.6k 33 91 301

Thank you for taking the time to explain this! :)

Aug 05, 2012 at 12:33 AM DNP

It seems that the docs are misleading. To say "move the object forward at 10 units per second" using 10 * Time.deltaTime isn't really 10 units per second, it's 10 units per fraction of a second, right? Maybe I'm misinterpreting...

Oct 10, 2013 at 03:40 PM thesfid

What? No. "Move the object forward at 10 units per second" is exactly correct and not misleading at all. That's the whole point of Time.deltaTime; to make movement framerate-independent.

Oct 10, 2013 at 03:56 PM Eric5h5

Ah, I see now. Thanks for elaborating @John LZG.

Mar 05 at 02:53 PM thesfid

Imagine if a half second passed during an update, meaning time.deltaTime would return a .5.

speedPerSecond * time.deltaTime.

10 speed and .5 deltaTime means = 10 * .5.

5 "move" in that half second.

Mar 05 at 12:52 AM John LZG
(comments are locked)
10|3000 characters needed characters left

If you want smooth animation across all hardware types you want to do things by time instead of by frame. By default Update is frame based. So you may have 30 frames a second or 60 frames a second or any variance from 0-hardware max.

Time.deltaTime allows you to move things not by frame (which would be too fast or too slow depending on your hardware). Time.deltaTime is essentially a fragment of a second or the time passed since the last frame. So if you move something with it in involved it will move them by time rather than by frame. Time.deltaTime is relative to the hardware you are on. If you aren't using it when you move things your game will play radically different on different hardware. Time.deltaTime returns a fraction of a second based on how long the last frame took for the hardware.

 The time in seconds it took to complete the last frame (Read Only).
 
 Use this function to make your game frame rate independent.
more ▼

answered Aug 04, 2012 at 08:52 PM

drawcode gravatar image

drawcode
436 1 5 5

Can you give me an example on a way it could be used?

Aug 04, 2012 at 08:55 PM DNP

The docs provide a code example for how it's used.

Aug 04, 2012 at 10:01 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

It basically updates by your frames per second then unitys witch is inconsistent.

its more consistent.

more ▼

answered Aug 04, 2012 at 10:51 PM

Tim A gravatar image

Tim A
335 71 73 97

could you explain a bit more?

Aug 04, 2012 at 11:06 PM DNP
(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:

x4112
x893
x450
x114

asked: Aug 04, 2012 at 08:42 PM

Seen: 16681 times

Last Updated: Mar 05 at 02:53 PM