奇怪的是/否其他行为

时间:2015-08-09 21:26:29

标签: unity3d

上下文

我正在制作一款手机游戏,玩家需要按照指定的顺序触摸对象。在名为clickOrder的List中确定正确的顺序。要确定播放器应该单击的当前对象,请使用currClickIndex。

问题

当触摸正确的对象时,调试文本将在瞬间显示“正确”,然后立即更改为“错误”。我不确定的是为什么只在触摸单个对象时执行if和else块。

代码

void Update()
{
    if (Input.touchCount == 1)
    {
        if (this.enabled)
        {
            Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);

            if (hit != null && hit.collider != null)
            {
                // check if the touched object is the correct one
                if (hit.collider.gameObject == clickOrder[MyData.currClickIndex])
                {
                    debug.text = "Correct";
                    MyData.currClickIndex++;
                }
                else
                {
                    debug.text = "Wrong";
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

触摸正确的物体后,即可执行以下操作:

MyData.currClickIndex++;

以有序的顺序向前移动,从那时起,先前正确的对象不再正确。但是你还在触摸它。

如果你想避免这种情况,你需要在之后继续前进你已经触摸过正确的物体。

if (there are touches and the correct object is being touched)
{
      set a flag;
}
else if (a flag has been set)
{
      MyData.currClickIndex++;
      reset the flag;
}