10. Pause Menu

Updated: 2018-08-31

Now that our gameplay is finished, we'll start implementing the menus.

In this part, we'll create a simple pause menu that allows the player to resume, exit to the main menu or quit the game altogether. This menu will pause everything in the background while it is shown.

1. Reactivate the pause menu object

Remember how we deactivated the pause menu object back when we implemented the HUD loader? That was because we didn't want it to pollute our view all the time even when not in pause. Now that we'll implement a proper pause system, we can re-enable it.

  1. Open the HUD scene
  2. Select the PauseMenu object
  3. Reactivate it:

2. HUD state machine

We'll create a simple global HUD state machine with two states: Playing and Paused.

  1. Select the HUD object
  2. Add a new state machine component
  3. Set its source to Embed
  4. Rename the start state to Playing
  5. Add a new flow state called Paused
  6. Add transitions back and forth between the Playing and Paused states

2.1. Paused State

Open the Paused state. We will only use the On Enter State event to:

  1. Set the Unity time scale to zero, effectively pausing the game
  2. Activate the PauseMenu game object

2.2. Playing State

In the playing state, we will do the opposite:

  1. Set the time scale back to one, effectively resuming the game
  2. Deactivate the PauseMenu game object

2.3. Pause Transition

To pause the game, we will wait for the the Escape keyboard button to be pressed

2.4. Resume Transition

To resume the game, we will allow two options:

  • Press escape again
  • Click the resume button

One powerful feature of Bolt events is that they can be listened to on other objects than the current one. Here, we'll use this to our advantage and listen to the On Button Click event from the ResumeButton object. Simply drag & drop the ResumeButton object (under the PauseMenu / Buttons in the hierarchy) to the Self field to replace the target.

After renaming the transitions properly, the final HUD state graph should look like this:

3. Main menu button

The main menu button should simply load the Menu scene when clicked.

  1. Select the MenuButton object
  2. Add a new flow machine
  3. Set its source to Embed
  4. Add the following units:

4. Quit button

The quit button is equally simple: it just needs to quit the application altogether when clicked.

  1. Select the QuitButton object
  2. Add a new flow machine
  3. Set its source to Embed
  4. Add the following units:

Quit is located under Codebase > Unity Engine > Application. Note that it doesn't work while in the editor (i.e. it won't exit play mode), but it works while in a build.

That's it! You should now have a functional pause menu in every scene.

Was this article helpful?
Be the first to vote!
Yes, helpful
No, not for me