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?
Answer by aldonaletto
Aug 04, 2012 at 11:55 PM
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;
// 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:
// 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;
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.
Thank you for taking the time to explain this! :)
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...
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.
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.
Ah, I see now. Thanks for elaborating @John LZG.
Answer by drawcode
Aug 04, 2012 at 08:52 PM
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.
Can you give me an example on a way it could be used?
The docs provide a code example for how it's used.
Answer by matriXcel
Jun 17, 2015 at 07:34 PM
Ok let me break this down to you like a boss say you are running the game at 100fps that would be 0.01secs/frame then deltaTime would be 0.01 and lets say you are moving a object 50m*Time.deltaTime on that one frame that would be 50m*0.01 it would move 0.5m/frame in proportion to 50m/sec ok now lets say you are still doing 50m*Time.deltaTime but on a slow computer that does 25fps that would be 0.25 secs/frame 50*0.25 would be 12.5/frame it is moving a greater distance because the frames are taking longer that is how Time.deltaTime compensates for hardware differentials. Time.deltaTime is lets say you have a countdown-=Time.deltaTime and the value of countdown is 10 it will subract 10 by 1 sec each sec but it is actually also subtracting it by the amount of time between frames if that makes sense that is why you will see floating point numbers.
Your example is good but your maths are wrong for the 25fps example.
If the game is running at 25fps, it is running 0.04 sec/frame, not 0.25 This would mean it would move 2.0m/frame, (i.e. 50*.04m/frame), not 12.5
I'm not being pedantic, it's just that I'm a newbie and couldn't fully understand your example until I tried to work it out for myself.
Answer by Tim A
Aug 04, 2012 at 10:51 PM
It basically updates by your frames per second then unitys witch is inconsistent.
its more consistent.
could you explain a bit more?
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
16 People are following this question.
Multiple Cars not working
Forward movement Slow? Delta.time? Frame Rate?
Digital Clock: Time.time > 60
Need Help With a NullReference Exception
What is Time.deltaTime?