質問

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