- Home /
[SOLVED] OnTriggerExit only called when object moves
I have my Player and Enemy objects on the same layer and I made them ignore collision with each other.
Physics.IgnoreLayerCollision(8,8, true);
My Enemy object has a Child that is on the Default layer, and I'm using a Collider on that object to detect when my Enemy collides with a Player (the Child object just has a Box Collider that is set right in front of the Enemy object)
The OnTriggerExit is being called properly when the Player moves out of the Enemy's Child Box Collider, but NOT when the Player remains standing still and the Enemy is the one that moves away (thus its Child collider moves away with it)
My OnTriggerEnter suffers from this because it doesn't detect another Enter if the last Exit wasn't called so... it's screwing up all the collision detection.
Is it because the parent object is on a layer that is ignoring collision that the child object is being affected this way? Either way, how do I fix this?
EDIT: I also Debug.Logged to make sure it was happening the way I explained. It will only call when Player object moves out of the collider, but not when the collider moves away from the player who is standing still.
EDIT2: I have a feeling it's because both Player and Enemy are ignoring collisions, and even if there's a Child object that's on another layer that accepts collisions, the physics engine doesn't respond well to it. I'll test this tomorrow and update.
EDIT3: Found the solution! It's because the Child Box Collider is 'sleeping' and must have a Kinematic Rigidbody attached in order to 'wake up' and calculate collisions continuously.
Unity docs on Rigidbody sleeping
This tells you:
If you have a sleeping rigidbody and you move a static collider (A collider without a Rigidbody attached) into the rigidbody or pull it from underneath the rigidbody, the sleeping rigidbody will not awake. So if you have a lot of Static Colliders that you want to move around and have different objects fall on them correctly, use Kinematic Rigidbody Colliders.
So basically if you want to calculate collisions with static colliders (normal capsule, sphere, box, etc) and want them to move around a lot then make sure to attach a Rigidbody and check 'Is Kinematic'
it can also be that your issue is with whats colliding with what
http://docs.unity3d.com/Documentation/Components/class-SphereCollider.html
use the little chart at the bottom to make sure yoru doing it right. It sounds to me like the actual problem is your player or object is "sleeping" and so it doesnt register collision, thats a thing the physics engine does automagically to keep calculations down, but in order for stuff to be woken it needs to move or something or have rigidbodies and stuff.