Delta Time VS Fixed Delta Time: Differences And Purposes
Time is an important part of many games. Unity’s Update events all run repeatedly, and you need a way to know how much time has passed since the last update so you’ll be able to keep certain actions consistent. This is where Delta Time comes in.
Delta Time, or Time.deltaTime, is the amount of time that has passed since the last frame. Whereas, Fixed Delta Time, or Time.fixedDeltaTime, is the amount of time that has passed since the last FixedUpdate call. Delta Time value varies between frames while Fixed Delta Time is always the same value.
There are several ways to use Delta Time. The common usage is making framerate-independent actions such as character movement or procedural animation.
Time.deltaTime VS Time.fixedDeltaTime
Delta Time (Time.deltaTime) | Fixed Delta Time (Time.fixedDeltaTime) | |
Related Update Events | Update, LateUpdate | FixedUpdate |
Value | Time passed since the last frame | Time passed since the last FixedUpdate call |
Consistency | Framerate-dependent | A fixed value |
The Difference Between Time.deltaTime and Time.fixedDeltaTime
The difference between Time.deltaTime and Time.fixedDeltaTime in Unity lies within their dependency to frame rate.
Time.deltaTime tells you the amount of time that has passed since the last frame update in seconds. This value is not consistent and can go up or down depending on the game’s frame rate, lag, and stuttering.
Time.deltaTime gives you the value in seconds as float type and is a read-only value, meaning you can’t manually set the value yourself.
private void Update()
{
Debug.Log("Time.deltaTime in Update():" + Time.deltaTime);
}
Code language: C# (cs)
Time.fixedDeltaTime, though, is different from its counterpart. It is the interval in seconds between physics and fixed frame updates, which is independent of the game’s frame rate.
Its value is based on a constant named “Fixed Timestep” and, by default, has a value of 0.02. The internal physics update runs on a reliable timer and you will always get this same value no matter what.
The Fixed Timestep value can be edited by going to Edit > Project Settings… > Time > Fixed Timestep. The default value is 0.02 seconds (which is 50 times per second).
private void Update()
{
Debug.Log("Time.fixedDeltaTime in Update(): " + Time.fixedDeltaTime);
}
private void FixedUpdate()
{
Debug.Log("Time.fixedDeltaTime in FixedUpdate(): " + Time.fixedDeltaTime);
}
Code language: C# (cs)
As you can see, Time.fixedDeltaTime doesn’t care about the interval between frames and will always give you the Fixed Timestep value.
This is also true even when your game is paused by setting Time.timeScale to 0:
private void Start()
{
Time.timeScale = 0;
}
private void Update()
{
Debug.Log("Time.fixedDeltaTime in Update(): " + Time.fixedDeltaTime);
}
private void FixedUpdate()
{
Debug.Log("Time.fixedDeltaTime in FixedUpdate(): " + Time.fixedDeltaTime);
}
Code language: C# (cs)
From the example above, the reason you can’t see the Time.fixedDeltaTime in FixedUpdate being printed in the console at all is because FixedUpdate doesn’t get called when the time scale is set to 0, but it still returns a value when called from other Update functions.
What Is Delta Time Used For?
The most common use for Time.deltaTime is for framerate-independent movement.
Why is that?
Here, let’s take a look at this example to start:
// A simple movement code inside the Update function
private void Update()
{
// Move the object that this script is attached to by 2 units along the X-axis
float newXPosition = transform.position.x + 2;
transform.position = new Vector3(newXPosition, transform.position.y, transform.position.z);
}
Code language: C# (cs)
This looks like there’s nothing wrong, right? This basically just moves the object by 2 units along the X-axis every time the Update function is called, right?
Well, let’s think about the definition of the Update function real quick here:
The Update function is called once every frame if the MonoBehaviour is enabled.
This is where the problem lies.
Because of how the Update function is called every frame, this means the number of times it’s called per second is not always the same.
When a game runs at the frame rate of 60 frames per second, Update gets called 60 times per second.
When a game runs at the frame rate of 120 frames per second, Update gets called 120 times per second.
If you run the code in the example above:
- At 60 frames per second, the object will move at the speed of 2 * 60 = 120 units per second.
- At 120 frames per second, the object will move at the speed of 2 * 120 = 240 units per second.
This creates an inconsistency. Plus, the fluctuation in frame rate caused by lag and stuttering will make the movement speed varies wildly.
To combat this, we need to make the movement framerate-independent by making use of Time.deltaTime.
Let’s fix the example code!
// A simple movement code inside the Update function
private void Update()
{
// Multiply the distance by the delta time
// to ensure the speed stays consistent across every frame rate
float distanceToMove = 2 * Time.deltaTime;
// Move the object that this script along the X-axis
float newXPosition = transform.position.x + distanceToMove;
transform.position = new Vector3(newXPosition, transform.position.y, transform.position.z);
}
Code language: C# (cs)
This is a simple fix. By multiplying the distance you want the object to move each Update by Time.deltaTime, this ensures that the object will always move at the same rate regardless of frame rate.
In this case, the object will move along the X-axis by 2 units per second.
Let me break it down a little more in case you’re still confused as to why we use Time.deltaTime:
At 60 frames per second:
– Update gets called 60 times per second
– This means it gets called every 1 / 60 seconds, or roughly 0.0167 seconds
– Therefore, the object will move by 2 * 0.0167 = 0.0333 unit per Update call
– Which is 0.0333 * 60 = 2 unit per second
At 120 frames per second:
– Update gets called 120 times per second
– This means it gets called every 1 / 120 seconds, or roughly 0.0083 seconds
– Therefore, the object will move by 2 * 0.0083 = 0.0167 unit per Update call
– Which is 0.0167 * 120 = 2 unit per second
That’s it! That is how we make the movement of an object framerate-independent!
Note: Do NOT use Time.deltaTime as a part of the calculation to set the value of RigidBody‘s velocity because the movement via the velocity value is already handled by the internal physics engine using Time.deltaTime and is already framerate-independent.
Attribution
Delta icon made by Freepik from www.flaticon.com