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;
}
}
}
有帮助吗?

解决方案

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;
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top