OnTriggerEnter not working

无人久伴 提交于 2019-12-02 11:09:07

问题


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:


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


来源:https://stackoverflow.com/questions/24581463/ontriggerenter-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!