简体   繁体   English

尽管检查了 OnTriggerEnter 和 OnTriggerExit,但它还是调用了两次

[英]OnTriggerEnter and OnTriggerExit called twice despite checking it

I have read a lot of questions about this problem but i couldn't solve it yet.我已经阅读了很多关于这个问题的问题,但我还没有解决它。 I have one ball with a Sphere Collider and an invisible wall to restart the ball when it passes through it (on the onTriggerExit method).我有一个带有 Sphere Collider 的球和一个隐形墙,可以在球穿过它时重新启动球(在 onTriggerExit 方法上)。 The problem is that i've not been able to solve it, even with a boolean to avoid entering the method.问题是我无法解决它,即使使用布尔值来避免进入该方法也是如此。

public class ballRestart : MonoBehaviour
{
    shootController instance;
    bool isColliding;
    // Use this for initialization
    void Start()
    {
        instance = Camera.main.GetComponent<shootController>();
        isColliding = false;
    }


    public void OnTriggerEnter(Collider col)
    {
        Debug.Log("TeEnter: " + isColliding);
        if (!isColliding)
        {
            isColliding = true;
            Debug.Log("TRIGGERED: " + isColliding);
        }

    }

    void OnTriggerExit(Collider hit)
    {
        Debug.Log("TeExit: " + isColliding);
        if (isColliding)
        {
            instance.initializeBall();
            isColliding = false;
            Debug.Log("exit");
        }
    }
}

OUTPUT:输出:

Here is the output of the Logs这是日志的输出

As you see, it enters twice each time the ball enters the collider and twice each time the ball exits the same collider.如您所见,每次球进入碰撞器时它进入两次,每次球离开同一个碰撞器时它进入两次。 I don't know what happens here.我不知道这里会发生什么。

I'm pretty sure you have 2 colliders on your object, each of them causes event triggering.我很确定您的对象上有 2 个碰撞器,每个碰撞器都会导致事件触发。 Check your components and child objects.检查您的组件和子对象。

Ps: I know it is old question, but my answer can be helpful for others. Ps:我知道这是个老问题,但我的回答可以对其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM