- Home /
Pause Script
A while ago i made this pause script:
private var gamePaused = false;
Screen.showCursor = false;
function Update(){
if (gamePaused == false){
if(Input.GetKey(KeyCode.P)){
gamePaused = true;
Screen.showCursor = true;
Time.timeScale = 0;
}
}
if (gamePaused == true){
if(Input.GetKey(KeyCode.O)){
gamePaused = false;
Screen.showCursor = false;
Time.timeScale = 1;
}
}
}
well. It works fine, but i really want it to be the same key to pause and unpause. When i change the KeyCode.O to KeyCode.P, it doesn't work. I think this is because when they are the same key, it pauses and then unpauses instantly, so it pretty much has no effect. I have put a debug log in both of the input if statements and they both work.
Try changing:
Input.Get$$anonymous$$ey($$anonymous$$eyCode.P)
to:
Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P)
And ofcourse change this line:
Input.Get$$anonymous$$ey($$anonymous$$eyCode.O)
to:
Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P)
To use the same key to pause and unpause use:
Screen.showCursor = !Screen.showCursor;
This flips the state.
Answer by kramcomposer · Mar 04, 2013 at 08:10 AM
Try this:
public class Pauser : MonoBehaviour {
public bool gamePuased = false;
//Just to make sure a previous scene didn't mess something up....
void Awake(){
Time.timeScale = 1;
Screen.showCursor = true;
}
void Update(){
if(Input.GetKeyUp(KeyCode.P)){
gamePuased = !gamePuased;
Time.timeScale = Mathf.Abs(Time.timeScale - 1);
Screen.showCursor = !Screen.showCursor;
}
}
}
Your answer
Follow this Question
Related Questions
What's wrong with this health bar GUI script? (C#) 1 Answer
health bar 2 Answers
Spinning Pointer that reacts when health drops 0 Answers
Health below zero but shows negative numbers 1 Answer
how to make a healthbar increase 2 Answers