# My take on Time.deltaTime and Frame-rate Independence

Hi Everyone,

I am sharing my understanding of Time.deltaTime and Frame-rate independence.

I am using pseudo code in the code snippets below for the ease of understanding.

– Animation and motion is basically a sequence of frames.
– What is a Frame? A Frame is a combination of the image to be displayed and the time it takes for the image to be displayed.
– Each frame is displayed on the screen until the next frame overwrites it.
– Frame Rate per second or FPS is the number of frames that are rendered (or displayed) per second.
– FPS depends on lot of different factors like the computer hardware, graphics and resolution settings, the scenes and images that are being rendered and overall how compute intensive your game is. The FPS can be different on different computers and it may also change from game to game or scene to scene. Simply put, the time each frame takes can vary.
– Time.deltaTime provides the time between the current and previous frame. It tells us how long the last frame took.
– In Unity, we have an Update() method that gets called every frame. This means Update() method is called multiple times in a second.

Now, that we understand what frame and frame-rate is, let’s take a look at the following example.

Let’s say your player needs to move along the X Axis. The following pseduo-code will move our player 5 units in the X direction every single frame.

//pseudo-code
int speed = 5
void Update()
{
//move.player(x,y,z)
move.player(speed,0,0)
}

Now you are playing this game on a computer that runs it at 60FPS.
Consequently the Update() method is called 60 times a second and the player will move 60x5 = 300 units in one second.

Your friend starts playing this game on his computer that runs it at 30FPS.
The Player will move 30 x 5 = 150 units in one second.

Now this is a problem because the player moves faster when you play this game on your computer compared to your friend’s computer. The Game experience is inconsistent and the player movement is dependent on the Frame-rate.

How do we solve this problem? We need to make the player movement Frame-rate independent. Let’s take a look how we can do that:

If 60 frames are rendered in 1 second, then 1 frame will take 1/60 seconds = 0.01666… second
Similarly, if 30 frames are rendered in 1 second, then 1 frame will take 1/30 seconds = 0.0333… second

This value is called the deltaTime i.e the time between the current frame and the previous frame.

Going back to our code, let us multiply our speed with the Time.deltaTime.

//pseudo-code
int speed = 5
void update()
{
//move.player(x,y,z)
move.player(speed * Time.deltaTime,0,0)
}

Now, When you play this game on a computer at 60 FPS, the update() method gets called 60 times in a second and the player will move 60 x 5 x 0.0166… = 5 units in one second.

When you play this game on a computer at 30 FPS, the update() method gets called 30 times in a second and the player will move 30 x 5 x 0.0333… = 5 units in one second.

We can see that no matter what the FPS is, the player will move 5 units in one second and this offers a consistent Gameplay experience.

In real world, it is not uncommon for FPS to vary when you are playing on the same computer. The time each frame takes can vary wildly and Time.deltaTime is a good predictor of the current frame time.

Without Time.deltaTime, the player movement would appear jerky. We can smoothen that out by using Time.deltaTime and it will make adjustments every frame and the player movement would appear smoother and less jerky.

Thanks!

4 Likes

Welcome to the community!

Amazing explanation of what Time.deltaTime is for and how frame rate can affect the player’s experience. Thanks for sharing this.

1 Like

Thank you for your kind words.

1 Like

Hello, every body! Everything was working fine in unity. But when I’ve tried to build game in webGL, I realised, that my Game objekt is to heavy and clumsy. Problem was solved by adding:
public float speed = 100; // After you can change value in unity - script window.
And for objekt movement I’ve used those changes:
if(Input.GetKey(KeyCode.Space)) { RocketBody.AddRelativeForce(Vector3.up * Time.deltaTime * speed); } if (Input.GetKey(KeyCode.A)) { transform.Rotate(Vector3.forward * Time.deltaTime * speed); } if (Input.GetKey(KeyCode.D)) { transform.Rotate(-Vector3.forward * Time.deltaTime * speed); }

That was an awesome explanation of Time.deltaTime and Frame-rate independence darthexter! Your example of walking on different framerate levels, 60 and 30, and how to make them consistent was very clear and easy to understand. I really feel like you’ve helped me grasp these topics better. Like Yee said, thank you very much for sharing!

1 Like