- Home /
OnTriggerEnter/Exit called unexpectedly
Hi, 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?
Is it possible that the NPC sight mesh is intersecting with the NPC's capsule? You can do a quick check in the console like this:
function OnTriggerEnter (col : Collider) {
print(col.gameObject.name);
}
unfortunately, i already checked, but no, that's not happening :\
This might seem silly, but you don't have duplicates of the script on the object, do you? Other than that, I'm stumped.
I would filter out the collisions by making sure they are indeed the player. Use Tags or Layers or w/e you wish, but make sure the collider entering the trigger is what you think it is.
Also add some simple print debugging like so:
void OnTriggerEnter( Collider col )
{
Debug.Log( col.gameObject.name + " is entering trigger " + this.gameObject.name );
}
And the same for exit.
That should tell you exactly what is going on, and should probably make the problem become obvious. Hope so anyways! Post with results if not!
Another thought... Is your cone collider a mesh? If so, that could be causing the immediate enter/exit behavior, if it has bad normals, is not a closed object, convex is wrong, etc...
Answer by senyedenes · Oct 14, 2016 at 10:23 PM
Hy,
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. :) (until you (and me as well) get a better solution)
Answer by belvita · Nov 29, 2013 at 01:53 PM
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