Understanding Time.deltaTime

Published in

--

Most of the people (including me) who start with Unity are having a problem with learning `Time.deltaTime.` To understand how it works, firstly, I am going to explain its definition and secondly, I am going to show a step-by-step example.

By definition, `Time.deltaTime` is the completion time in seconds since the last frame. This helps us to make the game frame-independent. That is, regardless of the fps, the game will be executed at the same speed. Let’s understand what this means:

Suppose, our game runs at 60 fps. It means that the game is updating 60 times per second, in other words, there are 60 frames.

Note: FPS stands for frames per second

Now, let’s have a look at to the time between frames:

Firstly, when the game starts, frame 1 is executing and when it finishes, frame 2 starts executing. When frame 1 ended, the frame 1 starts to be calculated till the end of the frame 2. Let’s see the image below to understand it:

• Before the frame 1 is executing, the `Time.deltaTime` is 0
• Then, the game continues and frame 2 is executing. So, the time between those 2 frames is 0.05 seconds.
• So, the `Time.deltaTime` is 0.05 seconds. That’s why we call it the time which passed since the last frame.

Note: Its value is changing continuously, for this example, I just used 0.05

As we understood what does that mean, let’s see the usage of it and why we are using it:

Always, when we want to move an object, no doubt we use an `Update()` method.

Note: Update() method is called once per frame

I want to show the difference between using `Time.deltaTime` and not using it.

I have a simple `player:`

And, I attached a `Test` script to it

The script code looks like:

• I declared a `float` `speed` variable and assigned 10 to it
• Then in `Update()` method, I used transform’s Translate method to move the object
• It moves the object forward and multiplies `forward` vector by speed
I didn’t multiply it with `Time.deltaTime` intentionally

Note: `Space.Self` is the parameter which is used to say to move the object local axis, not for world axis (We don’t need to know this, I just explained to the people who are curious about it)

Not Using Time.deltaTime

Let’s calculate our total movement when we are not using `Time.deltaTime`:

Suppose, we are testing our game on 2 different computers. In some game, one of them gets 20 fps, and the other one gets 500 fps.

Calculating Total Movement for First Computer

Firstly, let’s see the total movement for the first computer (20 fps):

In 1 second, the `Update()` method will be executed 20 times.

Our total movement will look like:

`total movement = 20 * Vector3.forward * speed`

Note that Vector3.forward is shorthand for writing new Vector3(0, 0, 1), so it moves by 1 unity.

If we calculate the total movement, we get:

`total movement = 20 * 1 * 10 = 200;`

Calculating Total Movement for Second Computer

Now, let’s see the total movement for the second computer (500 fps)

In 1 second, the `Update()` method will be executed 500 times.

Our total movement will look like:

`total movement = 500 * Vector3.forward * speed`

Now, if we calculate the total movement, we get:

`total movement = 500 * 1 * 10 = 5000;`

The player who is playing in 20 fps moved 200 unit and the other one who is playing in 500 fps moved 5000 unit. As you can see it is frame-dependent. The more fps you have, the more the player moves.

Using Time.deltaTime

Now, I changed the code so that the movement vector is multiplying by `Time.deltaTime` too:

Calculating Total Movement for First Computer

In 1 second, the `Update()` method will be executed 20 times

Time.deltaTime is approximately equal to 1 divided by fps count. Because, the game runs `20` times per second, so `1 / 20` is the time for each frame

`Time.deltaTime = 1 / 20` = 0.05

Our total movement will look like:

`total movement = 20 * Vector3.forward * speed * Time.deltaTime`

Now, if we calculate the movement:

`total movement = 20 * 1 * 10 * 0.05 = 10`

Calculating Total Movement for Second Computer

For 500 fps, in 1 second, the `Update()` method will be executed 500 times.

So, `Time.deltaTime` is equal to the following:

`Time.deltaTime = 1 / 500 = 0.002`

Our total movement will look like:

`total movement = 20 * Vector3.forward * speed * Time.deltaTime`

Now, if we calculate the movement:

`total movement = 500 * 1 * 10 * 0.002 = 10`

As you can see, when we use the `Time.deltaTime` the total movement is equal to 10. So, we see that it is `frame-independent.` Regardless of how fast the computer is, the game will be played at the same speed.

That was all for this article. I hope it was clear and you understood `Time.deltaTime.`

--

--

The Editor of the Star Gazers