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?

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.