OnTriggerEnter/Exit called unexpectedly

I have a capsule(the NPC body) with a cone mesh attached(their sight). This sight cone has attached a trigger collider and a kinematic rigidbody. This NPC is patrolling, so he walks and looks around. The problem is that I’m using another capsule(the player), which has both a collider(not trigger) and a kinematic rigidbody, to test the NPC sight(basically, I’m moving this character so to enter the field of view of the NPC), but OnTriggerEnter/Exit on the sight cone on the NPC is called unexpectedly: when the player capsule enters the trigger mesh of the NPC, suddenly onTriggerEnter and onTriggerExit get called twice each(and yes, in both onTriggerEnter and onTriggerExit i check that the object entering/leaving is the player collider). The same thing happens when the player collider leaves the trigger mesh of the NPC, onTriggerEnter and onTriggerExit get called twice each.

To make things a bit clearer, this appears in the Console:

Player Entering
Player Entering
Player Leaving
Player Leaving

Does anybody have any idea about what’s going on here?


This is because Physics can be called multiple times before each update method.
I had a very similar issue and my solution was something like that:

function OnCollisionEnter (collision : Collision) {
     if(!collisionAlreadyTracked) {
          // collision related code...
    collisionAlreadyTracked = true;

This simple statement basically prevents these events to be tracked multiple times per frame.
I guess there is a more sophisticated way to handle this, so it is just a quick workaround, but its working. :slight_smile: (until you (and me as well) get a better solution)

why dont u attach an empty game object with a box collider to your gameObject and use the box collider to check if OnTriggerEnter is called box colliders work far better than capsule colliders