Purpose of deltaTime

i am confused on why are we adding deltaTime to make it independent?
i know deltaTime is the time it took last frame. But why are we multiplying it here:
rigidBody.AddRelativeForce(Vector3.up * mainThurst * Time.deltaTime);

What is the logic here, and how is the deltaTime make our thrust independent? It is very conceptual, please help me to understand the idea :blush:

1 Like

Let’s say you had a frame rate that was really bad and getting progressively worse. It started at 10fps, next frame it was 6fps, then 3fps, then 2fps, then 1fps, etc.

In a line like this, you can see that Time.deltaTime is used to mark the passage of time:

timer += Time.deltaTime;

So let’s say we are using this code for a simple timer to keep track of how much time passes. This is considered “framerate independent” because it must keep track of time regardless of our frame rate.

Our timer variable starts at 0. The first frame is 10fps, or 10 frames per second, or 0.1 seconds per frame. Since it took 0.1 seconds, this is Time.deltaTime this frame. We’ll add that to the timer, so timer is now 0.1.

Next frame, this update runs again. This time the frame rate was worse (6 fps) and took 0.155 seconds. We’ll add that to timer, which is now 0.255.

Next frame, the update runs again. This time, the frame rate is 3fps, so it took 0.333 seconds. (The system knows this by checking the system clock each frame.) When the frame loads and update runs, Time.deltaTime will be 0.333. When this gets added to timer, the total value of timer will be 0.588.

Finally, when the next frame runs at a pathetic 2fps, Time.deltaTime will be 0.5 and will increase the total to 1.088. At this point, timer is above 1, so we know one second has passed! It took four frames, adding 0.1 seconds, 0.155 seconds, 0.33 seconds, and 0.5 seconds.

Because of the pathetic frame rate, our timer isn’t very exact, but your frame rates are likely to use much smaller values.

The point is that the system knows that 1 second has passed after 4 frames.

If you had a better, consistent frame rate, this one second of time might take 60 frames. By measuring how long each frame takes and adding it to timer, your system knows how many frames it took to reach that 1 second of real-time.

This isn’t ideal for a timer, because I can just check the system time myself with Time.time but its useful for character movement and such.

If you have a movement of 1, and move 1 each frame, your pathetic and worsening frame rate means you’ll have moved 4 units after 1 second (4 frames running 4 update cycles). But a friend at a consistent speed of 60fps will move 60 units in the same time. That’s not very fair is it? Even if it was just a 1 player game, your worsening framerate means your character will move less and less as the update runs less often.

But if you multiplied movement by Time.deltaTime you would move more in each update cycle than your 60fps friend, who has more frames to work with each second. It might take you 4 frames and take your friend 60, but in the end, you’ll have gone (approximately) the same distance after 1 second.

Time.deltaTime is effectively equal to 1/frames. Remember that speed is a matter of distance AND time, not just distance. “miles” is a distance while “miles per hour” is a speed. Another way of saying “miles per hour” is “miles * 1/hours”.

Notice any similarity between “miles * 1/hours” and “mainThrust * 1/frames”?

Basically when you add “* Time.deltaTime” to a distance, you are converting it to a speed. Instead of “mainThrust world units”, you get “mainThrust world units per frame”.

4 Likes

OMG THIS IS SO HELPFUL! Thank you!!!

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms