未调用 OnCollisionExit

为什么OnCollisionExit不被调用?我正在使用两者OnCollisionEnter,OnCollisionExit但不幸的是只有OnCollisionEnter被调用。


public bool HandleCollided = false;


public void OnCollisionEnter(Collision col)

{

    if(col.gameObject.name == "RightHandAnchor")

    {

        HandleCollided = true;

    }

}


public void OnCollisionExit(Collision col)

{

    if(col.gameObject.name == "RightHandAnchor")

    {

        HandleCollided = false;

    }

}


呼啦一阵风
浏览 254回答 5
5回答

呼唤远方

无法根据给定的代码片段判断您的代码为何无法运行 - 此代码取决于GameObjects'编辑器中每个检查器窗口的配置。此GameObject脚本附加到的对象和碰撞GameObject必须有一个Collider组件附加到它们中的每一个(例如,一个BoxCollider组件或一个SphereCollider组件)。两者都Colliders必须isTrigger禁用其复选框。此GameObject脚本附加到的 也必须Rigidbody附加一个组件。为了调试这种情况,Debug.Log()请在您的函数中添加语句。这通常是一种很好的做法,并且可能正在调用函数但条件语句不正确。以下是关于可能出现问题的一些额外想法:您可能正在更改GameObject代码中其他地方的碰撞名称。你可能正在破坏GameObject.可能是这两个函数都没有被调用并且HandleCollided在代码的其他地方被更改。可能是参数col不是您所期望的。public void OnCollisionEnter(Collision col){    Debug.Log("Collision Enter!");    Debug.Log(col.gameObject.name);}public void OnCollisionExit(Collision col){    Debug.Log("Collision Exit!");    Debug.Log(col.gameObject.name);}

芜湖不芜

不幸的是,使用 OnCollisionExit 不起作用,所以我改用 OnTriggerEnter 和 OnTriggerExit。我为这两个对象激活了“isTrigger”。  public void OnTriggerEnter(Collider col){    Debug.Log("entered");    if (col.gameObject.name == "RightHandAnchor")    {        HandleCollided = true;    }}public void OnTriggerExit(Collider other){    Debug.Log("exit");    if (other.gameObject.name == "RightHandAnchor")    {        print("No longer in contact with " + other.transform.name);        HandleCollided = false;    }}

一只萌萌小番薯

所以你说“有两个物体相互碰撞。一个附有球形对撞机,另一个附有箱形对撞机。其中一个物体也附有刚体。” 代码在哪一个?是的,这很重要!只有 1 个对象会跟踪出口,这意味着如果它是非运动学的,它将无法工作。

沧海一幻觉

尝试使用OnCollisionEnter()和OnTriggerExit()!例子:void OnCollisionEnter(Collision collision)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; if((collision.gameObject.GetComponent<AttributeManager>().attributes & doorType) != 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.GetComponent<BoxCollider>().isTrigger = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }private void OnTriggerExit(Collider other){&nbsp; &nbsp; this.GetComponent<BoxCollider>().isTrigger = false;}

江户川乱折腾

您需要将一个非运动学刚体附加到您的对象上以获取 OnCollisionExit 事件
打开App,查看更多内容
随时随地看视频慕课网APP