OnTriggerEnter called trice but OnTriggerExit only once

Hello,

I’ve a problem with management of trigger colliders. The context is a spaceship construction game where the player can construct floors tiles with artificial gravity fields. The gravity fields are actually trigger box colliders, the player character has a rigdbody and capsule collider :

The problem is keeping track of the gravity fields the player character is in. Sounds trivial, but poses problems. the player character’s script is along the lines of this :

private var malGravityFields : ArrayList = new ArrayList();

function OnTriggerEnter( _collider : Collider ) 
{
	if( _collider.tag == "GravityField"  )
	{
		malGravityFields.Add( _collider );
	}
} 

function OnTriggerExit( _collider : Collider ) 
{
	if(  _collider.tag == "GravityField" )
	{
		malGravityFields.Remove( _collider );
	}
}

The problem is that the code does not work as expected; the player character happens to be considered inside a gravity field he has left since long. I kept track of which gravity field trigger is entered and left in which order : It turns out that when said “wrongly considered inside” problem occurs, the trigger usually had been entered twice, then left, then reentered. A typical case is:

  • time = 30.98: The Player character enters the trigger
  • time = 31.00: The Player character enters the same trigger
  • time = 31.20: The Player character leaves the same trigger
  • time = 31.28: The Player character enters the same trigger
  • time = 35.00: The Player character is physically far away from the trigger but still considered inside

Note that this seems only to happen when the player collider barely grazes the trigger collider.

Is this a Unity bug, or am I doing something wrong ?

Does anything in this thread help?

http://forum.unity3d.com/threads/93682-How-To-Get-Number-of-Objects-in-Trigger

Jeff

Are the triggers overlapping by any chance? it could explain why there are more enters than exits.

There is also another problem. If you are using compound colliders (lots of triggers grouped by one game object) then you will need to loop through contact points for correct collision information, otherwise it won’t register properly.

Apparently, the solution in this case was to check “Convex” checkbox, but in my case I’m dealing only with capsule and box colliders.

What I listed were enters and exits of one specific collider. I kept track of which collider was entered and left with by adding “_e”+Time.time respectively “_x”+Time.time to the collider’s name when entering resp. leaving. In the mentioned case, this resulted in the name “GravityField_e30.98_e31_x31.2_e31.28”. So, it was clearly this very collider which was entered thrice and left once.

Oh, and I don’t use compound colliders. (But thanx for the info anyway, I may need it sometime.)

Might be the same as this:
http://forum.unity3d.com/threads/77452-3.2-gives-no-OnTriggerExit-when-collider-is-parented

Silly question but how are you moving your rigid body? its important. Also what loop is it in, fixedupdate or update?

  1. if you move them or position them you HAVE to use moveTo or forces, otherwise it won’t understand what you’re doing for several iterations.
  2. if it isn’t done in fixedupdate, similar problems.
  3. don’t use deltatime on physics if you are already.

@HiPhish : My situation is rather different - however, a common point is that the parent is changed on trigger enter. (In my case the new parent after entering the trigger is a “spaceship” object the gravity triggers are also childs of.) Maybe this is where the problem comes from?

One hypothesis may be the following :When you change the common parent of nontrigger collider and trigger which barely touch one another, this can “sneak the collider outside without alerting the guards”. In other words, a “geometrically inside” collider can become “geometrically outside”, but as the global position didn’t change, the OnTriggerExit is not triggered. Kind of a “tunnel effect”. This could explain why my collider sometimes enters twice, and why it occasionally enters but doesn’t exit, despite being moved away.

I will create a test project and do some tests based on this hypothesis, to boil down the problem.

@Hippocoder : I’m actually using forces in FixedUpdate (at least I think so - I’m at home right now and the code is at the office). However, I thought the trigger events are also supposed to be fired if you move a rigidbody manually ?

I did some tests, see http://forum.unity3d.com/threads/77452-3.2-gives-no-OnTriggerExit-when-collider-is-parented?p=609299#post609299