- Home /
how to correctly check for a collision with a character controller
the player is a character controller, the enemy is a rigidbody.
in a script attached to the enemy I use OnCollisionEnter() to react when it hits the player. however, it barely ever notices a hit. it seems it often notices it when I use addforce on the enemy, but never when I just walk into it.
So I'm asking if anyone knows a way to correctly check for a hit in the enemy script, not in the player script, searching this site, that was most often the remedy. but I just can't beleive that there wouldn't be a way to correctly check it outside of the player. I mean otherwise the character controller is just completely useless.
Try making an object inside your enemy and make it a trigger. $$anonymous$$ake that a child of your enemy. Then just give it a tag name and use the OnTriggerEnter() function ins$$anonymous$$d of OnCollisionEnter(). Usually the triggers work better for me then using collision.
works even worse :s god the character controller sucks, I guess I should just use a rigidbody for the player
...Have you tried adding a different collider to the enemy in addition to the rigidbody? That should help, and you will still be able to use "OnCollisionEnter()".
it already consisted of mutiple colliders as children of the rigidbody
Answer by aldonaletto · May 26, 2012 at 05:48 PM
The collision system has some weird features: OnCollision events are generated only when the rigidbody hits the character, not the other way around (to be more precise, sometimes they may be generated, but only when the rigidbody is also moving).
To detect character->rigidbody collisions, use the OnControllerColliderHit event. It's sent only to the character script, but you can create your own OnCharacterRigidbodyHit event in the rigidbody script and call it via GetComponent:
Rigidbody Script (let's suppose it's called RigidbodyScript.js):
function OnCharacterRigidbodyHit(point: Vector3, normal: Vector3, other: Transform){ // this function will be called when the character hits the rigidbody; // in this example it will receive the rigidbody's hit point and normal, // and the transform of the character that hit the rigidbody }
Character Script:
function OnControllerColliderHit(hit: ControllerColliderHit){ if (hit.rigidbody){ // if a rigidbody was hit, try to get its RigidbodyScript: var rScript: RigidbodyScript = hit.rigidbody.GetComponent(RigidbodyScript); // if such script was found, call its OnCharacterRigidbodyHit event: if (rScript) rScript.OnCharacterRigidbodyHit(hit.point, hit.normal, transform); } }You can modify the fake OnCharacterRigidbodyHit to have any other arguments, case the ones suggested above aren't what you need.
NOTE: Many guys criticize the CharacterController, but I think it's yet the best solution for characters. From my experience, any kind of rigidbody based character I've tried to implement gave me way more headaches than the old-and-not-so-good CC...
thanks, but I had already tried that, the problem is that it somehow triggers to much (like 10-20 times per actual hit). And it causes a small framedrop. And when I comment out the original OnCollision() it still doesn't always work, and when I leave them both (so nothing commented out), there are cases when it triggers both (so double effect). hm, I think I'll just do the CD manually.
Are you using this exact code for OnControllerColliderHit? This event is generated every physics cycle due to the collisions between the character and the ground, thus we must ignore the undesired events - that's why the code checks if a rigidbody was hit before doing anything. Anyway, if you want a rigidbody character, try this: http://www.unifycommunity.com/wiki/index.php?title=RigidbodyFPSWalker
Your answer
Follow this Question
Related Questions
How do I use a power up to change the character I'm controlling? 3 Answers
Determinism of collision judgment of Character Controller 0 Answers
How to slow character controller down when colliding with a wall? 2 Answers
Basics of Collision help. Working with character controllers. 1 Answer
Gun collision problem 3 Answers