Random Code: Pause Menu

Have you ever been creating a game when you thought to yourself, “Hey, I want to pause my game”, then you realised that you’re so silly that you don’t know how? Then this is the script for you!

private var paused: boolean = false;
private var mouseX: MouseLook;
private var mouseY: MouseLook;

function Start()
{
    Screen.showCursor = false;
    Screen.lockCursor = true;

    mouseX = GameObject.FindWithTag("Player").GetComponent(MouseLook);
    mouseY = GameObject.FindWithTag("MainCamera").GetComponent(MouseLook);
}

function Update()
{
    if(Input.GetKeyDown(KeyCode.Escape))
    {
        if(!paused)
        {
            paused = true;
            mouseX.enabled = false;
            mouseY.enabled = false;
            Screen.showCursor = true;
            Screen.hideCursor = false;
        }
        else
        {
            paused = false;
            mouseX.enabled = true;
            mouseY.enabled = true;
            Screen.showCursor = false;
            Screen.hideCursor = true;
        }
    }
}

function OnGUI()
{
    Time.timeScale = 1;
    if(paused)
    {
        Time.timeScale = 0;
        if(GUI.Button(Rect(Screen.width / 2 - 75, 200, 150, 25), "Back To Main Menu"))
        {
            Application.LoadLevel("StartMenu");
        }
        if(GUI.Button(Rect(Screen.width / 2 - 75, 250, 150, 25), "Back to Game"))
        {
            paused = false;
            mouseX.enabled = true;
            mouseY.enabled = true;
            Screen.showCursor = false;
            Screen.hideCursor = true;
        }
    }
}

This script isn’t too hard to get to grips with. First, we declare a few variables, and make them private so another script can’t come along and vandalise them. One to store whether we are saved or not, and two for each of the MouseLook scripts attached to the standard Unity first person controller. When these are initialised in the Start function, the script will look for the tags “Player” and “MainCamera” – in my project, these are the the tags assigned to my main camera and the actual player character, each of which have a MouseLook component. It also hides the cursor from view, and locks it to the centre of the screen.

Then, if the player presses the Escape key, it checks whether the game is paused. If it isn’t, then it pauses the game, disables the MouseLook scripts and frees the cursor; if it is paused, the opposite happens. In the function OnGUI(), which is called every time the frame is drawn and is used to draw stuff on-screen using the basic Unity GUI features, the time scale is set to zero when the game is paused, which stops all movement (this basically is the part of the script that actually pauses the game). Then it draws two GUI buttons, centred in the middle of the screen horizontally; one returns the player to the main menu, and the other unpauses the game. Pressing the first button will run Application.LoadLevel(“StartMenu”); which loads a level named “StartMenu”. The latter just unpauses the game.

PauseMenu1

This is the result when you pause the game, a basic menu with two buttons. In any case, I hope this helps, have fun coding!

Advertisements