Time for action – adding code to pause the game Scene
In BeginState
, we're going to set timeScale
to zero to pause the Scene. Then before switching to PlayState
, we'll set it back to normal speed. Perform the following steps:
Modify
BeginState
in two places as shown in the next screenshot.Save the file.
What just happened?
An analysis of the code shown in the preceding screenshot is as follows: On the BeginState
class
Line 14: Time.timeScale = 0;
This line makes time in the game to stop
Line 29: Time.timeScale = 1;
This sets the game speed back to normal when switching to
PlayState
Click on Play again. Now the Cube GameObject is suspended in air. Click on the Press to Play button and everything is back to normal and you're in PlayState
.
Notice how the button disappears when you change States. Once again, this shows the beauty of the State Machine. You don't have to use a bunch of if
statements to make things appear or disappear. Each State determines the control you want and what shows on the...