# Time.deltaTime?

Can somebody explain to me a bit how Time.deltaTime works? Yes, i have seen the scripting reference page. But it doesnt really explain much. Im trying to learn, so can someone please help me out here?

Time.deltaTime is the time passed since last frame. If you want to move something at a constant velocity speed, for instance, multiply speed by Time.deltaTime and you will get exactly the distance moved by the object since last Update (remember: distance = velocity * time):

``````var speed: float = 10;

function Update(){
// move the object forward at speed units per second:
transform.Translate(transform.forward * speed * Time.deltaTime);
}
``````

You can use Input.GetAxis(“Vertical”) to control the movement:

``````var speed: float = 10;

function Update(){
// move the object forward/backward at speed units per second:
transform.Translate(transform.forward * speed * Input.GetAxis("Vertical") * Time.deltaTime);
}
``````

The same applies to the “Horizontal” axis, but not to “Mouse X”, “Mouse Y” and “Mouse ScrollWheel”! That’s because the axes Vertical and Horizontal return constant values in the range -1…1 when you press the buttons WSAD, while Mouse X, Y and ScrollWheel return the mouse/scrollwheel movement since last frame - thus these values already take the time into account.

Another use for Time.deltaTime is to decrement or increment time at constant rates. If you want to decrement a 10 seconds timer, for instance, you could do this:

``````var timer: float = 10;

function Update(){
timer -= Time.deltaTime;
if (timer <= 0) print("10 seconds ellapsed");
}
``````

You can use Time.deltaTime inside Update, coroutines or FixedUpdate (Unity returns the time since last FixedUpdate in this case), but you should not use it inside OnGUI because this function is called several times between frames.

If you want smooth animation across all hardware types you want to do things by time instead of by frame. By default Update is frame based. So you may have 30 frames a second or 60 frames a second or any variance from 0-hardware max.

Time.deltaTime allows you to move things not by frame (which would be too fast or too slow depending on your hardware). Time.deltaTime is essentially a fragment of a second or the time passed since the last frame. So if you move something with it in involved it will move them by time rather than by frame. Time.deltaTime is relative to the hardware you are on. If you aren’t using it when you move things your game will play radically different on different hardware. Time.deltaTime returns a fraction of a second based on how long the last frame took for the hardware.

``````The time in seconds it took to complete the last frame (Read Only).

Use this function to make your game frame rate independent.
``````

Ok let me break this down to you like a boss say you are running the game at 100fps
that would be 0.01secs/frame then deltaTime would be 0.01 and lets say you are moving a object 50mTime.deltaTime on that one frame that would be 50m0.01 it would move 0.5m/frame in proportion to 50m/sec ok now lets say you are still doing 50mTime.deltaTime but on a slow computer that does 25fps that would be 0.25 secs/frame 500.25 would be 12.5/frame it is moving a greater distance because the frames are taking longer that is how Time.deltaTime compensates for hardware differentials. Time.deltaTime is lets say you have a countdown-=Time.deltaTime and the value of countdown is 10 it will subract 10 by 1 sec each sec but it is actually also subtracting it by the amount of time between frames if that makes sense that is why you will see floating point numbers.

Can we say that Time.deltaTime is a helper?

Let’s say we want to move something. Now I write pseudo code…

``````position += (1 meter * time.deltaTime;)
``````

On my computer time.deltaTime is “1”. Because it took 1 second to show the next frame.

This means:

1 meter * 1 = 1 meter. So time.deltaTime does not really do anything.

We have moved 1 meter in 1 second.

Now let’s say we have a very slow computer.

You will notice that it’s slow because time.deltaTime is “2” (not 1 like on my computer). It took 2 seconds until the next frame was shown.

Now what happens?

1 meter * 2 = 2 meters

We have moved 2 meters in 2 seconds.

That is still 1 meter per second.

So that’s very good.

We still have the same result. The same applies to a very fast computer where time.deltaTime would be 0.001.

You will notice that you will always move 1 meter in 1 second if you use “* time.deltaTime”.

I remember once seeing a really old piece of software, running in a more modern computer, that was used to show an animation. In this more modern computer, the animation was running so fast that we could not understand how it was supposed to be.

Every computer has different processing power, and will render a different amount of frames per second. For example, if you change the position of an object one pixel per frame, the object will look like moving at different speeds depending on the FPS. Delta time makes this more consistent, because is a value taken from the system’s clock. Multiplying the movement by the delta time instead of incrementing by an absolute value, will make the movement speed consistent across computers.

Time.deltaTime is the time it took for completion of the last frame. Using it make the action independent of time frames.
As an example:

``````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 ( 60 frames in 1 second. Time for each frame is 0.016666666(i.e. 1/60). 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)

So, Time.deltaTime is used for actions that need to be independent of frame rate. The object now moves similarly on devices with lower fps or higher fps.