In this tutorial, we will see what Time.deltaTime is in Unity and how to use it.



Video Transcript

Hello everyone! In this tutorial, we will see what Time.deltaTime is in Unity and how to use it.

This beginner-friendly tutorial is perfect for those who are new to scripting in C# within Unity, as the topic is fundamental and deserves to be well understood, since you will encounter it in many scripts and tutorials.

The tutorial was created using Unity 2022, but Time.deltaTime has been around for several versions of the software and will likely continue to be in the future.

Time.deltaTime is a variable that represents the time elapsed, in seconds, between the last frame and the current frame (or, if you prefer, the time it took to complete the previous frame).

It is used to make movements and animations frame rate-independent, usually by multiplying an object's speed in units per second by Time.deltaTime in each frame.

This way, the speed per frame will vary based on the frame rate, ensuring the same speed on devices with different performance levels.

Let's see a practical example of what we just described, with a very simple case: the movement of an object along an axis at a constant speed. Insert an object into the scene, framing the scene from above with an Isometric view.

Give the object a C# script, calling it, for example, movement, then create a Vector3 to indicate a displacement of one unit (which we want to cover in one second):

private Vector3 obVelocity = new Vector3(0.0f, 0.0f, 1.0f);

We know that Update is called at each frame; however, we do not know how many frames per second the game will run, as this varies based on several parameters.

If our frame rate were consistently 50 frames per second, we could modify obVelocity to (0.0f, 0.0f, 0.02f), moving 0.02 units with each call to Update (and, therefore, each frame), resulting in a displacement of one unit per second.

Since we don't have this possibility, in Update we will write:

transform.position += (obVelocity * Time.deltaTime);

Save the script, return to the Unity editor to observe the result: the object moves linearly, maintaining a speed of one unit of the scene per second.

In conclusion: to move an object at a certain speed (expressed as scene units per second), set this speed in a variable and, inside the Update function, multiply it by Time.deltaTime, as I showed you earlier.

Now let's look at another example.

In the scene I'm using now, the Main Camera has been given a movement script that detects the arrow key presses (using Input.GetAxis for Horizontal and Vertical).

The values of Horizontal and Vertical range from -1 to 1 for both, so if I directly multiplied these values by Time.deltaTime in the Update function of the script, I would achieve a displacement of one unit in the scene per second, but this solution does not provide the ability to adjust the speed.

To solve this problem, the simplest thing to do is define a private variable with the Serialized modifier, which can be seen and modified in the Inspector when testing the game in the Editor.

In the example I'm showing you on video, this variable is called "speed" and initially has a value of 12.

In the Update function, the movement is managed through controller.Move, because the character is associated with a Character Controller component that is moved using the Move function.

The function is called with a rather long parameter, divided into two parts.

In the first part, the displacement along the X and Z axes is calculated based on the user's input, stored in the move variable (which is a Vector3).

In the second part, the force of gravity is calculated when a jump is performed.

Time.deltaTime is used both in the calculation of movement along the axes and in the vertical movement due to gravity, but we will ignore the second part (which would require a longer discussion) and focus on the first.

The movement vector move is multiplied by Time.deltaTime, as seen with the cube in the first example, but it is also multiplied by the float variable speed.

I then switch to the Unity editor and start the game to show you how it is possible to change the character's movement speed by modifying, in real time, the value of the Speed parameter in the script tab.

This way, it is possible to calibrate the speed for the start of the game.

Since this speed is multiplied by Time.deltaTime, we know it will be the same on various devices, regardless of the execution frame rate.

As "speed" is a variable in the code, it will also be possible to modify it during the game, based on certain events.

For example, the player might drink a potion that allows them to move at twice the speed or, on the contrary, to proceed more slowly than usual.

The key point is that this speed will be consistent across different devices, as it is independent of the frame rate, thanks to Time.deltaTime.

In conclusion, in this video we have seen two simple examples of using Time.deltaTime.

Soon, I will be publishing a video about rotations using Slerp, where Time.deltaTime will be employed to achieve constant-speed rotations, regardless of the frame rate.

That's all for this tutorial! See you soon!

This website is intended solely to showcase some of my work and has no promotional purpose. Please note that I am not currently seeking - nor will I respond to - requests for custom work, consulting services, or any other form of professional collaboration.


EXTENDED PRIVACY AND COOKIE USAGE POLICY