C# 在Unity3D中检测两个标记以确定玩家死亡

C# 在Unity3D中检测两个标记以确定玩家死亡,c#,methods,unity3d,tags,C#,Methods,Unity3d,Tags,我在做一个经典的陷阱,两堵墙合在一起砸碎玩家。为此,我创建了两个单独的标记“Crush”和“Crush1”,并为每面墙指定了一个标记。这是我的玩家类代码: void OnCollisionEnter(Collision other) { if (other.transform.tag == "Crush" && other.transform.tag == "Crush1") { Die (); } } 我只希望玩家在两堵墙同时碰到玩家时

我在做一个经典的陷阱,两堵墙合在一起砸碎玩家。为此,我创建了两个单独的标记“Crush”和“Crush1”,并为每面墙指定了一个标记。这是我的玩家类代码:

void OnCollisionEnter(Collision other)
{
    if (other.transform.tag == "Crush" && other.transform.tag == "Crush1")
    {
        Die ();
    }
}

我只希望玩家在两堵墙同时碰到玩家时被摧毁。当我移除参数中的一个标记时,该方法工作正常(只是不是我想要的方式)。我相信这里有一个简单的解决办法,我只是没看到。谢谢你的帮助

您可以使用以下代码创建一个脚本,并将其附加到两个墙对象:

public class WallScript : MonoBehaviour
{
    [HideInInspector]  //Optional Attribute
    public bool collidedWithPlayer;

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.name == "Player")
        {
            collidedWithPlayer = true;
        }
    }

    private void OnCollisionExit(Collision collision)
    {
        if(collision.gameObject.name == "Player")
        {
            collidedWithPlayer = false;
        }
    }
       //We don't want "collidedwithPlayer" to remain true if either of the walls touches the player before both do so we call "OnCollisionExit" to set it to false.
}
然后在播放器脚本中执行以下操作:

  private WallScript wall1Script;
  private WallScript wall2Script;

  private void Awake()
  {
      wall1Script = GameObject.Find("Wall1").GetComponent<WallScript>();
      wall2Script = GameObject.Find("Wall2").GetComponent<WallScript>();
  }

你知道,当玩家碰到两堵墙时。 函数
OnCollisionCenter
将执行两次。 第一次,
other.transform.tag==“Crush”
为true。 第二次,
other.transform.tag==“Crush1”
为true。 在
OnCollisionCenter
中,
other
只有一个标记。
如果
tag1!=tag2

@Christiopher G正确地说,当旗子击中你时,你需要存储旗子(并停止击中你)

我认为他的代码有点臃肿,但由于不需要新的类,您当前的代码很容易扩展到您想要的地方

注:Unity目前不可用,因此未经测试

bool wall1 = false;
bool wall2 = false;

void Update()
{
    if( wall1 && wall2 )
    {
        Die();
    }
}

void OnCollisionEnter(Collision other)
{
    if (other.transform.tag == "Crush")
    {
        wall1 = true;
    }
    else if(other.transform.tag == "Crush1")
    {
        wall2 = true;
    }
}


void OnCollisionExit(Collision other)
{
    if (other.transform.tag == "Crush")
    {
        wall1 = false;
    }
    else if(other.transform.tag == "Crush1")
    {
        wall2 = false;
    }
}
  private void Update()
  {
      if(wall1Script.collidedWithPlayer && wall2Script.collidedWithPlayer)
      {
         //Do Something here
      }
  }
bool wall1 = false;
bool wall2 = false;

void Update()
{
    if( wall1 && wall2 )
    {
        Die();
    }
}

void OnCollisionEnter(Collision other)
{
    if (other.transform.tag == "Crush")
    {
        wall1 = true;
    }
    else if(other.transform.tag == "Crush1")
    {
        wall2 = true;
    }
}


void OnCollisionExit(Collision other)
{
    if (other.transform.tag == "Crush")
    {
        wall1 = false;
    }
    else if(other.transform.tag == "Crush1")
    {
        wall2 = false;
    }
}