0

我正在尝试扩展这个滚球教程以包含一个计时器,并允许用户通过点击触摸板来重试,无论他们赢了还是时间用完了。

如果时间用完(// case A下),但如果玩家获胜(// case B下),这将按预期工作,因为水龙头似乎没有被识别。在这两种情况下都会出现结束消息,因此它肯定会到达这些部分,但我猜该程序没有到达带有评论的部分,// reset on tap但不确定。

任何想法表示赞赏。

我的PlayerController脚本:

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);
    }
}
4

1 回答 1

1

在SetCountText方法中放入一个Debug.Log,输出count count的值。你可能永远不会达到 12 分。确保您的所有收藏品都带有“拾取”标签。

更新Update您应该在方法 中监听玩家输入。FixedUpdate如果在两次FixedUpdate调用之间发生,作为固定更新的一部分执行的任何其他函数都将丢失玩家输入。

所以改变你的UpdateandGameOver方法如下:

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;
    }

}
于 2015-08-29T19:19:12.187 回答