Input not recognised from touchpad

血红的双手。 提交于 2019-12-11 16:49:49

问题


I am trying to extend this Roll-a-Ball tutorial to include a timer and allow the user to try again by tapping the touchpad whether they win or run out of time.

This works as expected if the time runs out (// case A below) but not if the player wins (// case B below), where the tap does not seem to be recognised. The end message appears in both cases so it is definitely reaching those parts but I'm guessing that the program is not reaching the section with the comment // reset on tap but am not certain.

Any ideas appreciated.

My PlayerController script:

void Start ()
{
    timeLeft = 5;
    rb = GetComponent<Rigidbody>();
    count = 0;
    winText.text = "";
    SetCountText ();
}
void Update()
{
    if (!gameOver) {
        timeLeft -= Time.deltaTime;
    }
    timerText.text = timeLeft.ToString ("0.00");
    if(timeLeft < 0) {
        winner = false;
        GameOver(winner);
    }
}
void GameOver(bool winner)
{
    gameOver = true;
    timerText.text = "-- --";
    string tryAgainString = "Tap the touch pad to try again.";
    if (!winner) { // case A
        winText.text = "Time's up.\n" + tryAgainString;
    }
    if (winner) { // case B
        winText.text = "Well played!\n" + tryAgainString;
    }
    // reset on tap
    if (Input.GetMouseButtonDown (0)) {
        Application.LoadLevel(0);
    }
} 
void FixedUpdate ()
{
    float moveHorizontal = Input.GetAxis ("Mouse X");
    float moveVertical = Input.GetAxis ("Mouse Y"); 
    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);    
    rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other) 
{
    if (other.gameObject.CompareTag ( "Pick Up")){
        other.gameObject.SetActive (false);
        count = count + 1;
        SetCountText ();
        if (!gameOver) {
            timeLeft += 3;
        }
    }
}   
void SetCountText ()
{
    if (!gameOver) {
        countText.text = "Count: " + count.ToString ();
    }
    if (count >= 12) {
        winner = true;
        GameOver(winner);
    }
}

回答1:


Put a Debug.Log in SetCountText method, and output the value of count count. You are probably never hitting the 12 point mark. Make sure all your collectibles have the tag " Pick Up".

Update You should listen for player input in Update method. FixedUpdate and any other functions that execute as part of Fixed Update will miss the player input if it happens between two FixedUpdate calls.

So change your Update and GameOver method as follows:

void Update() {
    if (gameOver) {
        if (Input.GetMouseButtonDown(0)) {
            Application.LoadLevel(0);
        }
    } else {
        timeLeft -= Time.deltaTime;
        timerText.text = timeLeft.ToString("0.00");
        if (timeLeft < 0) {
            winner = false;
            GameOver(winner);
        }

    }

}
void GameOver(bool winner) {
    gameOver = true;
    timerText.text = "-- --";
    string tryAgainString = "Tap the touch pad to try again.";
    if (!winner) { // case A
        winText.text = "Time's up.\n" + tryAgainString;
    }
    if (winner) { // case B
        winText.text = "Well played!\n" + tryAgainString;
    }

}


来源:https://stackoverflow.com/questions/32289354/input-not-recognised-from-touchpad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!