- Home /
Question by
RoyGeeBoy · Oct 10, 2014 at 11:57 AM ·
gamekeypresspause menukey pressed
Trigger Pause Screen when escape key is pressed?
I want a pause menu to show up when I press the escape key. Here is my script.
using UnityEngine; using System.Collections;
public class PauseMenu : MonoBehaviour {
private int buttonWidth = 200;
private int buttonHeight = 50;
private int groupWidth = 200;
private int groupHeight = 170;
bool paused = false;
void Start ()
{
GameObject.Find ("Player").GetComponent<MouseLook>().enabled = true;
Screen.lockCursor = true;
Time.timeScale = 1;
}
void OnGUI()
{
if(paused)
{
GUI.BeginGroup(new Rect(((Screen.width/2) - (groupWidth/2)),((Screen.height/2) - (groupHeight/2)), groupWidth, groupHeight));
if(GUI.Button (new Rect(0,0,buttonWidth, buttonHeight),"Main Menu"))
{
Application.LoadLevel("MainMenu");
}
if(GUI.Button (new Rect(0,60,buttonWidth,buttonHeight),"Resume Game"))
{
Application.LoadLevel(Application.loadedLevel);
}
if(GUI.Button (new Rect(0,120,buttonWidth,buttonHeight),"Quit Game"))
{
Application.Quit();
}
GUI.EndGroup();
}
}
void Update ()
{
if(Input.GetKeyUp (KeyCode.Escape))
paused = togglePause();
}
bool togglePause()
{
if(Time.timeScale == 0)
{
GameObject.Find ("Player").GetComponent<MouseLook>().enabled = true;
Screen.lockCursor = true;
Time.timeScale = 1;
return(false);
}
else
{
GameObject.Find ("Player").GetComponent<MouseLook>().enabled = false;
Screen.lockCursor = false;
Time.timeScale = 0;
return(true);
}
}
}
Comment
"I want a pause menu to show up". What does it do now?
From what I see, your code is right. If you want to resume the game, just use paused = togglePause();
ins$$anonymous$$d of Application.LoadLevel(Application.loadedLevel);
if the resume button is clicked.
Your answer

Follow this Question
Related Questions
Changing Version of Game 0 Answers
Messy Code or Clean Code 0 Answers
getting udp package info inside unity (GlovePIE) 0 Answers
Help Touch inputs 0 Answers