Why we use Time.deltaTime in Unity?

Hello everyone, welcome back.

Have you ever wonder what exactly is Time.deltaTime or you have used it in the past and faced difficulty to truely understand what it does behind the scene?

Today, I will try to explain what Time.deltaTime exactly does, hopefully at the end, you guys will have a solid understanding of it.

This will be the beginner friendly post. Let me know in the comments if you guys would like to see more of these in future, once in a while. Basically small things that I faced difficulty to grasp when I started.

With out further ado, Let's get started.

Concept theory




Let's start with basic theory, Unity has a class named Time, which provides an interface to get time related information.

It has a property called deltaTime, hence Time.deltaTime.

What is deltaTime? It is a time interval between your two consecutive frames.

It will mostly be used within the Update(). As we all know Update() gets called every frame, so if you have 300 FPS, it will call 300 times in one second.

Practical example

I truly believe that, a person will better understand the concept if you teach them how they can use that concept in real life scenarios, so let's try to understand it practically by moving a cube.

Create a default cube, create a script, call it CubeMovement, attach it on our cube and double click to open it.

Here we will define our speed, so let's create a field. private because I don't want any other class to access it, then type of our field will be float and give a name speed, with a default value of 1. We want to access the speed from the inspector so let's throw in SerializeField attribute as well.

Then in Update(), let's just move our cube so let me just use transform.Translate() and will pass 0 on the X and Y and speed in our Z.

It will look something like this.

using UnityEngine;

public class CubeMovement : MonoBehaviour {
[SerializeField] private float speed = 1f;

private void Update() {
transform.Translate(0, 0, speed);
}
}

Here we are telling Unity to move our cube in the forward direction, 1 unit each frame, again because Update() gets called every frame. So our calculation is frame dependent right now.

Assume I have a machine capable of 100 FPS, so my cube will move 100 unit over the time of 1 second. If someone has machine capable of only reaching 60 FPS, their cube will only move 60 units over the same timespan.

So the code that we have written acts differently on different machines, which is not ideal. It's in fact far from being ideal.

To fix that we will just multiply our speed with Time.deltaTime. You have most probably heard the term "We have made it frame independent", while it is technically correct, I think it is a bit ambiguous, better term to use is "We have made it time dependent".

using UnityEngine;

public class CubeMovement : MonoBehaviour {
[SerializeField] private float speed = 1f;

private void Update() {
transform.Translate(0, 0, speed * Time.deltaTime);
}
}

Basically we are saying Unity that move our cube, amount of units specified in speed, over the course of 1 second. Let me give you a quick example here.

Imagine I have a machine running at 10 FPS, so my delta time will be 0.1, our speed is 1 so 1 * 0.1 = 0.1 it will move 0.1 unit in 1 frame, And in 1 second it will run 10 times so 0.1 * 10 = 1 unit, so 1 unit over the course of 1 second.

Now you guys have a faster machine running at 20 FPS, so the delta time will be 0.05, in 1 frame you will move 1 * 0.05 = 0.05 units, and in 1 second it will run 20 times so 0.05 * 20 = 1 unit.

So now our code will act same no matter if it runs on 100 FPS machine or 30 FPS machine. We have basically converted our speed from units/frame to units/second and 1 unit = 1 meter in Unity so meters/second.

That's it. I hope you guys can now confidently use Time.deltaTime in your own code with the better understanding.

Trouble understanding the concept? Check out this video!


Thank you so much for reading.

Comments