- Home /
How does deltaTime exactly help faster computers?
This may seem like an easy question, but for some reason it caused me to rethink for a moment. I've searched through previous questions, and none of them really explained exactly how this works. I was reading through the Unity 3D Scripting tutorial, and came across the input part:
To accept input, you simply have to get the Axis your trying to move, and the game will render your input onscreen in the form of, well, movement. This goes like this:
transform.Translate(Input.GetAxis("Horizontal", 0, "Vertical"); ///// This makes perfect sense. But when it started talking about Delta Time, this was added: ///// transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime*speed, Input.GetAxis("Vertical")*Time.deltaTime*speed);
So, as far as I can tell, your essentially getting a copy of the amount of time since the last frame was rendered, then multiplying it by the actual speed at which you want to move the GameObject. This threw me off. How could doing this help framerate on faster computers? If anything, wouldn't it make the frames render too quickly?
Here's an example:
Player presses Right Arrow(1)
1 is multiplied by Time.deltaTime(let's say, half a second for a slower computer)
.5 is now multiplied by the actual speed you want to go by
Maybe I'm looking at this from the wrong angle, but wouldn't this mean that the time since the last frame rendered would be greatly increased for the next rendering?
speed = 3.0 .5 * speed = 1.5
This would help slower computers, but I don't see how it wouldn't hurt faster ones. I've searched this entire site for help, but have only found questions regarding what deltaTime is, and inconsistencies while using it. I may just be overthinking this, so I'm sorry if it's a large question if it's only a little thing, but I suppose it'll help anyone else who gets confused, right? :)
Answer by syclamoth · Oct 19, 2011 at 08:54 AM
I think you're misunderstanding what the word 'help' means in this case. The point of multiplying all your values by deltaTime is to make sure that numbers for force, transformation, rotation etc. are framerate-independent, which is to say that in one second, an object will move the same distance (given a per-update translation) whatever framerate the computer is rendering at.
Using deltaTime does not affect framerate in any way except one- it will slow things down a very very very small amount because of that one extra multiplication in there. I should mention that the difference is extremely small.
An example would be, if your game was going at 2 frames per second, and you wanted an object to move by 6 units per second. In your Update function (which gets called exactly once per frame), you would multiply the distance (6) with the amount of time in the previous frame (in this case, about 0.5 seconds). This way, in ever one of the two frames ever second, the object will move 3 units- which add up to 6 every second.
However, when you turned the graphics settings down because you didn't like that slideshow, the framerate went up to a good amount like 60. In this case, the deltaTime will be something more like 0.016 seconds- when you multiply 6 by that, you get 0.1. Then, every frame, your object will move by 0.1 units, and then over 60 frames it will have moved 6 units, like you wanted!
Let's look at it from the other side. If you didn't use delta time, on your slow computer you would move by 12 units per second. However, on your fast mode, you'd be moving at 360 units per second!!
Basically, using deltaTime is not for performance optimisation, it is about game simulation accuracy and consistency. You can never know at compile time how long it will take between frames, so deltaTime allows you to take framerate into account when you are attempting to move an object (or do anything, really) at a constant rate.
I don't even know what I was thinking. Thank you for clearing this up for me, I believe I mistook the statement to mean it would just render everything faster. I suppose this is another reason why not to try to comprehend code when your tired from exams. :)