如何检查是否满足两个条件?

时间:2019-10-18 01:56:34

标签: c# unity3d

我正在尝试让玩家走到GameObject上,如果他们在该对象上,然后按空格键,则显示调试日志。

private void OnTriggerEnter2D(Collider2D other)
{
   if (other.gameObject.tag == "Level_1" && Input.GetKeyDown(KeyCode.Space))
   {
        Debug.Log("Both Conditions Reached");
   }
}

1 个答案:

答案 0 :(得分:1)

仅当他们在进入对象时按住空格键时才会触发。最好在按下空格键时检查它们当前是否正在与对象碰撞。 (或在“更新”中同时检查两者)

在播放器对象中调用它:

private void OnTriggerEnter2D(Collider2D other) {
   if (other.gameObject.tag == "Level_1") {
        player.isInside = true;
   }
}

private void OnTriggerExit2D(Collider2D other) {
   if (other.gameObject.tag == "Level_1") {
        player.isInside = false;
   }
}

并使用它来检查空格键:

 public void Update() {
      if (Input.GetKeyDown(KeyCode.Space) && player.isInside == true) {
        Debug.Log("Both Conditions Reached");
     }
 }