- Home /
How does Time.deltaTime provide smoother physics functions? (Frame rate question)
Working through the 'Roll-A-Ball' tutorial provided by Unity; I had to create a cube that rotates and multiply the transform's rotation by Time.deltaTime which returns the time elapsed from the last frame. My question is centered around the frame rates and why multiplying Vector3 values times the previously elapsed frame rates returns a 'smoother' rotation than multiplying all values times a small float like 0.03f which is close to a-lot of the frame rates rendering time. I have also tried it without multiplying anything and just making rotate by the 'Update()' function alone basically; and my cube began rapidly changing its position; extremely fast. Can someone please explain the rotation process that is occurring in between each frame?
using UnityEngine;
using System.Collections;
public class Rotate : MonoBehaviour {
// Use this for initialization
// Update is called once per frame
Vector3 num = new Vector3();
void Update ()
{
transform.Rotate(new Vector3(15,30,45) * Time.deltaTime);
// Versus: transform.Rotate(new Vector3(15,30,45) * 0.03f);
Debug.Log (num);
Debug.Log (Time.deltaTime);
}
}
Thanks in advance!
Answer by tanoshimi · Jul 25, 2016 at 06:23 AM
Imagine you're watching a plane flying overhead on a summer's day. You close your eyes for a moment and then reopen them - the plane has moved slightly forward in the sky. Now you close your eyes again, but for longer this time, and then reopen them. The plane has moved forward further this time than before. You feel so relaxed, you close your eyes again, and fall asleep for a few minutes. When you open your eyes, the plane is but a tiny dot in the distance.
Each time you open your eyes, that's a "frame". The length of time you closed them before looking is Time.deltaTime. Like the example here, that can vary each frame, which is why the amount an object has moved since last frame should be made proportional to Time.deltaTime.
Your proposed solution of using a small float of 0.03 would be equivalent to the plane moving the same, small distance forward however long it had been since you last looked.... which would be odd, right?
So essentially the frames are demonstrating the following behavior with frames?
|Frame1| -----Frame load( 0.02 secs)-------------- |Frame2 (Time.deltaTime(0.02 secs)) | -----------Frame load( 0.04 secs )-------------------------- |Frame3(( Time.deltaTime(0.04 secs))|
Answer by IronarmGames_LLC · Jul 25, 2016 at 06:27 AM
@BrandonW you have to think about what Time.deltaTime
is. It is
the time in seconds that it took to complete the last frame
So that means if 1 frame took .01 seconds to complete, thats what itll multiply by, but, you usually also want to multiply Time.deltaTime
by another value to act as a dampener
.
Heres an example:
this.transform.position += this.transform.forward * (5f * Time.deltaTime);
In that line of code we will smoothly move our object in its forward direction that is not entirely dependent on frameRate
, its based on the math we are doing within the logic. In what you were wanting to do, you are basically multiplying the rotation that will happen multiple times every frame by an additional value instead of making it dependent on the actual framerate.
Does that make sense?
So essentially the frames are demonstrating the following behavior with frames?
|Frame1| -----Frame load( 0.02 secs)-------------- |Frame2 (Time.deltaTime(0.02 secs)) | -----------Frame load( 0.04 secs )-------------------------- |Frame3(( Time.deltaTime(0.04 secs))|
"dampener"? That's misleading. That value doesn't "damp" anything. It's just the speed the object should move at. However when using deltaTime that speed is given in "units per second" ins$$anonymous$$d of "units per frame".
If you do
transform.position += transform.forward * 5f;
Your object will move 5 units in one frame. At 60 fps that's 300 units per second and at 30 fps that's 150 units per second. So without deltaTime your speed depends on the framerate
when doing
transform.position += transform.forward * 5f * Time.deltaTime;
Your object will move 5 units per second regardless of the framerate
I also don't get what you mean by "multiplying the rotation" and what happens multiple times per frame?