OnTriggerEnter/Stay/Exit2D() Not Working Consistantly

I currently have Unity 5.3.4f1 installed, and I’m having some difficulty with getting consistent results from OnTriggerEnter/Stay/Exit2D.

I have a player character and an enemy character, each have a rigid body and collider. Both have Kinematic unchecked, and I have tried different combinations of interpolate, sleep and collision detection modes.

In my environment, I have ground tiles each with a BoxCollider2D and no RigidBody.

As a child to the player character, I have a GameObject with a BoxCollider2D set as a trigger and a simple attack script. In the hero’s animation, I enable the collider during attacks, then disable it after the attack. The script attached to the child is simply:

void OnTriggerEnter2D(Collider2D target) {Debug.Log(“Enter” + target.gameObject.name);}
void OnTriggerStay2D(Collider2D target) {Debug.Log(“Stay” + target.gameObject.name);}
void OnTriggerExit2D(Collider2D target) {Debug.Log(“Exit” + target.gameObject.name);}

When I strike at the enemy, it never registers an Enter, Stay or Exit call. I have tested many times, tweaking every parameter I can find. Not once.

When I strike at a ground tile, it registers Enter and Stay once each. It occasionally registers Exit once. However, if I strike the same ground tile again, it will not register any Enter, Stay or Exit calls. Even if I hit another tile and come back, any tile that’s been hit won’t issue calls until I stop playback.

I feel like I must be making some kind of really dumb rookie mistake, so if anyone can provide some advice on how to get more consistent results I’d really appreciate it! Thanks in advance.

The trigger messages are generated by the rigidbody component. A collider (no matter if it’s a trigger or a normal collider) can’t detect collisions / intersections on their own. Only Rigidbodies can. The messages should be generated on the gameobject that has the rigidbody attached as all child colliders are used as compound colliders.

By attaching a kinematic rigidbody to your child object you allow the child to generate trigger messages on it’s own. See the tables at the bottom of this page which object can collide or generate trigger messages. In any case at least one rigidbody is required.

To answer your follow up question:

Are there any consequence or best
practices to keep in mind when putting
a rigid body on the child of an object
with a rigid body?

Yes, if you attach a rigidbody as a child of any other moving object, this rigidbody should be kinematic. Non-kinematic rigidbodies are simulated in worldspace and should therefore never be a child of another moving object as this movement will disturbe the simulation of the rigidbody.