Journal 146 — Unity Project Development, 2.5D Shooter
Objective: Add Pause, Restart, and Continue Menu
Over the past couple weeks, I have been working on various improvements to my project. In this article, I want to go over how I added a pause menu in game in order to be able to restart and continue the game.
I’ll start with a new UI panel in the main UI Canvas and I will set this active and inactive based on button selections to restart and continue.
I’ll first have the Pause menu inactive and then only by pressing the “P” key will the Pause menu show.
I’ll add a new key command for pause to my PlayerActions.
In my GameManager script, I will check the playerInput if they press the Pause key, then set the Pause menu active, and pause the game.
Pausing the game is pretty straightforward in Unity, by setting the time scale to 0 or 1. Note this only works with movement for example that uses Time.deltaTime. Also, we want to set the AudioListener to be paused true or false. See this article for additional background.
In the UI Manager, I can handle the OnClick() button events to restart and continue the game, and also set the pause menu active and inactive.
To restart, I’ll need to do a few things besides just reloading the active scene:
- Clear the enemy movement waypoints
- Do not destroy the UI Canvas
- Make sure the Pause menu is set back to inactive.
- Restore the Time.timeScale.
To continue the game, I will simply set the timeScale to 0 and pause the audio, then when the continue button is pressed, set the timeScale back to 1 and unpause the audio, and set the pause menu back to inactive.
Now, let’s see the results.
Thank you for your time!
I still have several more improvements to make to build out all the features I would like.