- Home /
I do not understand Time.deltaTime
This would be my 1st question on Unity Answers, I'm usually able to find something relevant by googling. I'm making a standalone physics based first person puzzle platformer as a stand alone on PC. My problem is that when my framerate is much higher in the 'fastest' version of the build then everything speeds up to the point where the Benny Hill theme would not be ill suited.
Not only is it scripts I've made, but the speed of the '1st person controller' from the 'standard assets' is significantly increased. I then used 'Application.targetFrameRate' to see what happens when the framerate is decreased and sure enough everything slows down significantly.
I mentioned the controller, but there's also this rotation script:
function update(){
if (NS) { transform.Rotate(Vector3(x, y, z)* Time.deltaTime); }
if (HS) { transform.Rotate(Vector3(xSped, ySped, zSped)* Time.deltaTime); }
}
and a camera fade script:
function OnGUI(){
alpha += (fadeDir * fadeSpeed) * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
GUI.color.a = alpha;
GUI.depth = drawDepth;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
}
I'm more than a bit confused, Time.deltaTime doesn't seem to be helping. Would it be safe to just set a target framerate and hope that the game runs close enough to it that it doesn't feel too strange?
Any advice?
Answer by aldonaletto · May 11, 2013 at 03:07 PM
Time.deltaTime has no meaning inside OnGUI, because this function is called multiple times each frame. You should move the alpha calculation to Update and use the alpha value inside OnGUI, like this:
private var alpha: float;
function Update(){
alpha += (fadeDir * fadeSpeed) * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
}
function OnGUI(){
GUI.color.a = alpha;
GUI.depth = drawDepth;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
}
Outside FixedUpdate, Time.deltaTime is the time elapsed since last frame. Inside FixedUpdate, it's a fixed value (20mS, by default). Multiplying the desired angle by Time.deltaTime makes it frame rate independent - for instance:
transform.Rotate(Vector3(0,90,0) * Time.deltaTime);
This code should rotate the object at 90 degrees per second about the Y axis.
The First Person Controller moves at constant speed, independent of the frame rate. If it's moving too fast, something is very wrong in your project - create a new project and verify if things work as expected in it.
Thanks for such an insightful response! So the rotation script should be in Update and not FixedUpdate, correct?
And yes, I feel like the me from 6 months ago must have done something to the project settings to create these problems.
Thanks again for taking the time to help.
It's better to keep the rotation code inside Update: at low frame rates, FixedUpdate is called multiple times between frames; at high frame rates, several frames may be rendered between FixedUpdate calls. This may result in choppy movement or rotation, since the amount moved or rotated each frame isn't precisely related to the last frame duration.
Ah of course it was in OnGui !
I didn't even noticed ! Thank goodness for Aldo.
Your answer
Follow this Question
Related Questions
FixedUpdate and Time.deltaTime slowe 2 Answers
Locate cause of massive frame rate drop 0 Answers
Performance of game depends on computer's power option? 0 Answers
Animation of terrain causes drastic performance drop (fps) 0 Answers
Using time.deltatime to move from coordinate to coordinate is too fast? 1 Answer