It is very important for a Unity Developer to have a sound understanding of Time.deltaTime. Let us learn the basics of Time.deltaTime in Unity3D with some examples.
What is Time.deltaTime?
Time.deltaTime is the completion time in seconds since the last frame. It is read only. Therefore you cannot modify it! This property provides the time between the current and previous frame. But what does that mean?
We are aware of the Update method in Unity3D. Time.deltaTime in layman terms would be the time lapse between two update methods. Let’s say there are 10 lines of code in the Update Method. So, Time.deltaTime would determine how much time does it take to execute the lines of code in one frame. This property is a variable value as its value would depend upon how much time one frame takes to execute which further depends upon how much lines of code needs to be executed.
Points to note:
- FixedUpdate uses fixedDeltaTime instead of deltaTime.
- Unity can call OnGUI multiple times per frame. Therefore, it is not advisable to rely on Time.deltaTime inside OnGUI method.
Time.deltaTime in relation with fps
FPS is frames per second and the time taken for completion of one frame is Time.deltaTime. In other words, we can say,
fps = 1/Time.delaTime
or
Time.DeltaTime = 1/fps
Therefore, when fps is 60, Time.deltaTime is = 0.0166666 seconds
Usage of Time.deltaTime in Unity3D
Creating a Timer
private float timer = 0.0f;
void Update()
{
timer += Time.deltaTime;
//Game Over after 60 seconds
if(timer > 60){
//Game Over
}
}
Smooth Translation of an object independent of Frame Rate
Let us use static speed for translation of an object for movement in x-axis, lets say speed = 5 as below:
private int speed = 5;
void Update()
{
transform.Translate(speed , 0 , 0);
}
Lets test the lines of code on a device with fps equal to 60 (i.e. 60 frames in one second). The object moves 5 units in 1 frame(as speed=5). The distance moved by object in 60 frames or 1 second is 60*5 = 300 units
Now, lets test this code on a device with fps equal to 30. The object still moves 5 units in 1 frame as speed = 5. However, the distance moved by object in 30 frames or 1 second is 30*5 = 150 units.
Therefore, using static value, an object on a device with higher fps moves faster than that on a lower fps device, which is not what is desired.
Lets see what happens when we use Time.deltaTime.
void Update()
{
transform.Translate(Time.deltaTime , 0 , 0);
}
The above line of code gives a smooth translation in x-axis as the object will always move independent of the Time Frame i.e. if the fps falls low or high, the translation is linear.
Lets say the fps is 60, i.e. 60 frames in one second. Time for each frame is 0.016666666 as explained formerly. The distance moved by object in 1 second is 1 unit(0.0166666*60 frames)
If the fps is 30, i.e. 30 frames in one second, time for each frame is 0.03333. The distance moved by object in 1 second is 1 unit(0.03333*30 frames)
Therefore, the object moves over the same distance irrespective of the device performance.
So, Time.deltaTime is used for actions that need to be independent of frame rate.
Happy Coding!