【问题标题】:Input not recognised from touchpad触摸板无法识别输入
【发布时间】:2015-08-29 18:19:35
【问题描述】:

我正在尝试扩展此Roll-a-Ball tutorial 以包含一个计时器,并允许用户通过点击触摸板来重试,无论他们赢了还是时间用完。

如果时间用完(下面的// 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);
    }
}

【问题讨论】:

    标签: c# android unity3d virtual-reality gear-vr


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 正确显示消息Well Played..,肯定达到12。
    • 我看到了问题。您应该始终检查更新中的玩家输入。您正在 OnTirggerEnter>SetCountText->GameOver() 中检查鼠标点击。因此,您基本上是在收听 FixedUpdate 调用中的输入。我将在一分钟内用解决方案编辑答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多