- Home /
OnCollisionEnter won't fire?
I have a player which is using the ThirdPersonController from the 3D platformer tutorial. I've also added a SphereCollider to my player and I added the OnCollisionEnter function to the ThirdPersonController script. Which looks like
function OnCollisionEnter(collision : Collision)
{
print("OnCollisionEnter");
}
I've also added an empty game object to my scene and attached a Rigidbody and a BoxCollider to it. When my player walks into the empty gameobject's BoxCollider, nothing print out in the debug console.
If I stop the scene and uncheck gravity and start the scene, I can see the BoxCollider start to float up. Now if I run into it, OnCollisionEnter is printed out. I only get this behavior if I uncheck gravity on the Rigidbody before I start the scene. If the scene is already running and I uncheck it, the BoxCollider doesn't start to float up and walking into it produces no output.
Why won't OnCollisionEnter fire with gravity turned on? Also, why does disabling gravity cause the object to float? Shouldn't it stay in place until some force acts on it?
edit: I still can't figure this out. I don't see why 2 colliders can't collide with each other. And for some reason, in this case a collider and rigidbody with a collider attached won't even collide with each other.
Try putting
if(collision.collider){
print("sdhfshdfhsdf");
}
Inside the function.
Answer by aldonaletto · Feb 03, 2012 at 10:45 PM
This a recurrent problem: OnCollisionEnter events are generated when a rigidbody hits a CharacterController, but not when the character hits the rigidbody (except in some rare occasions). You must use OnControllerColliderHit in the character script (the event isn't sent to the rigidbody script).
NOTE: The "rare occasions" I mentioned are when the rigidbody is already moving and the character hits it - that's why you get OnCollision events when the rigidbody is floating.
Answer by Artifactx · Feb 03, 2012 at 10:31 PM
Hey you should try adding a rigidbody to your gameObjects and try this:
function OnTriggerEnter (other : Collider) { print("I just hit something"); }
Your answer
Follow this Question
Related Questions
OnCollisionExit2D - NotWorking? 0 Answers
Detecting Collisions with Sphere Collider 1 Answer
CharacterController and Normal rigidbody Box Collider Collision issues 0 Answers
How to make a progress bar go up after two objects collide? 0 Answers
Colliders not inheriting the Transforms of changed animation states(Run to Crawl) -Unity3D, 0 Answers