Grasping the concept of Time.deltaTime

Hello,

I know this question has been asked before, but I am still confused about the Time.deltaTime function. So I understand how it makes something frame independent. I don’t understand why we can’t use an alternate method to achieve the exact same result.

For example, suppose the frame rate for the scene is 24 fps. Why can’t we just divide the value (which we would otherwise multiply with Time.deltaTime) by 24? Would it not achieve the same result?

So, for example, the original code looks like this:

function Update () {
    // Move the object 10 meters per second!
    var translation : float = Time.deltaTime * 10;
    transform.Translate (0, 0, translation);
}

But I think it can be written like this to behave no differently:

function Update () {
    // Move the object 10 meters per second!
    var translation : float = (1/24) * 10;
    transform.Translate (0, 0, translation);
}

Of course, if the frame rate is not 24, replace the 24 with whatever else you want.

The only advantage I see of Time.deltaTime is that if we change the frame rate, it’s a little bit easier to keep the code more flexible. But it can be achieved by setting the frame rate in a global variable and dividing by it instead of a raw number (i.e. 24).

So what is the flaw in my logic here? Can the frame rate change on different devices or something?

Thank you so much!

Because the frame rate depend on how your computer is performing at a certain time. Sometimes my game runs at 80fps and 10 secs layers it’s at 100fps. Also if the fps dropped to 1 u can see why u need Time.deltaTime

The frame rate isn’t a fixed value - on the contrary, it varies a lot, even in a single machine! There are many reasons: Unity does a lot of things between frames, which may take a variable time; the frames themselves take variable time to be rendered, depending on their complexity; the computer has its own internal tasks to consume time, and so on. Furthermore, different machines produce very different frame rates: the same scene runs at 9 fps in my notebook (crappy Intel video card) and at 110 fps in my desktop (cheap ATI HD 4550 video).

As Dasherz said, framerate is not fixed. It is dynamic and can change during runtime. Time.deltaTime ensures that the game plays in the same speed, even if the framerate drops form 120 to 12.

If you don’t want to use it, you have the option of using FixedUpdate() instead of Update(). This will be executed at a fixed speed, so there is no need for Time.deltaTime, but performance will suffer.