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!

Big Spiky Death Balls – Project Spikes 27/09 Update

I’ve finished the fourth level and improved the existing three slightly with features from this update. Firstly, I’ve added large spike balls. They’re rather deadly and tend to fall on your head.

spikes-15They were pretty fun to experiment with with positioning, along with Unity’s physic materials (especially the bouncy one) to make them hop around the stage like kangaroos on meth. I also added mini posters/ billboards that warn the player of dangers ahead, and one at the start of each level to tell you the level number.

spikes-16

If you wondered what the player looks like in my scene view, he’s a bright purple capsule. He’s a little bit feminine and proud of it.

I’ve used some existing objects to devise new traps for the player – you’ll have to find them for yourself though! Along with this comes a new health bar, which is much more colourful (and hence, better) than before; it’s a nicer way of displaying remaining lives and health. There’s a few new features such as a start screen and pause screen; the latter of the two is accessed through the escape key. The cursor stays frozen in the centre of the screen and hidden while the game in unpaused, meaning it won’t stray from the game window and interfere with other applications.

spikes-17Also, as some players pointed out, some moving platforms used to stutter when stood on, but this has been fixed now by updating their position in the FixedUpdate() function rather than Update(), as the former is ran before the latter. Turrets have also been improved, as their bullets get destroyed after coming into contact with any collider, not just the player, and they aim better.

CHANGELIST / TR;DR
-Added large spiky balls of death and physic materials
-Added a fourth level
-Added small warning posters
-New trap variants
-New health bar
-New start menu
-New pause menu -  press "esc"
-Fixed bullets only being destroyed after hitting player and turrets' aiming
-Fixed stuttering platforms

For next update, I will be finishing up World 1 and perhaps starting work on World 2. The fifth and final level in this world will be a bit bigger than the previous ones, and feature a puzzle in order to advance forward. So far, it looks promising.

If you want to try the game out, the link is the same as it always has been: play it here! And thanks for playing!