Domanda

When the player hits P button, it will pause the game but it won't work

What am I doing wrong here ? I declared the private var ispaused

private var ispaused = false;

Here is my code

function Update () {
if(Input.GetKeyDown("p")){
if(!ispaused){
Time.timeScale = 0;
ispaused = true;
}
if(ispaused){
Time.timeScale = 1;
ispaused = false;
}
}
}
È stato utile?

Soluzione

EDIT: You're right, the key mapping was correct after all.

The second if statement needs to be an else if. Now you're going through both if statements each time you hit p, effectively setting the boolean to true and then immediately false again.

Here's the working code:

function Update () {
    if(Input.GetKeyDown("p")) {
        if (!ispaused) {
            Time.timeScale = 0;
            ispaused = true;
        } else if (ispaused) {
            Time.timeScale = 1;
            ispaused = false;
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top