OnTriggerEnter not working

2019-03-02 17:23发布

public var enemy:GameObject;

enemy = GameObject.FindGameObjectWithTag("enemy");

function OnTriggerEnter(other:Collider)
{
   if(other.gameObject.tag == "enemy")
   {
      Debug.Log("Dead");
      Destroy(gameObject);
   }
}

This script is attached to a prefab arrow that gets instantiated. The enemy has a circle collider and the arrow has a box collider. The arrow has on IsTrigger checked. What have I done wrong? Both gameobjects have a rigidbobdy2D attached.

1条回答
贪生不怕死
2楼-- · 2019-03-02 17:34

If you use the 2D physics engine, you need to use the 2D functions:

function OnTriggerEnter2D(other: Collider2D) 
{
    if(other.tag == "enemy")
    {
        Debug.Log("Dead");
        Destroy(gameObject);
    }
}
查看更多
登录 后发表回答