Trouble with Time.Timescale

I’m trying to toggle time scaling using a GetKeyDown check. The trouble is that when I press the key, nothing happens. I have no errors in the console, and I’ve tested that the GetKeyDown and the if statements are working by adding a print event, which works fine. I’m also moving the object along its Y axis using transform.Translate, just to see whether timescaling is working.

Here’s the code:

function Update ()
transform.Translate (0, 0.02, 0);
if ( Input.GetKeyDown ( "s" ) )
		if ( Time.timeScale == 1.0 )
			Time.timeScale = 0.2;  //slow time down
			print ( "Time slowed." );
			Time.timeScale = 1.0;  //regular speed
			print ( "Time Restored." );

Like I said, no errors, and the printing is working just fine. So what’s the problem here?

Your movement is not framerate-independent, so it won’t be affected by Time.timeScale. Update() always runs as often as it can - irrespective of timeScale - and you’re moving your transform by 0.02 units every time it is called.

Your translate should be made frame rate independent as follows:

transform.Translate (0, 0.02 * Time.deltaTime, 0);

(You might also need to scale the 0.02 up to around 1 instead)

For more info: Time.timeScale seems doesn't work with Update - Unity Answers

Basically your script is saying if time scale != 1 then set it to 1 :slight_smile:
you should set the scale to 0.2 on key down and set it back to 1 on key up.