- Home /
How to get collisions on Character controller?
I have the player with a character controller on him and an object with a box collider and a rigid body on it. What function do I need to use to get the collision between the two? I have tried OnCollisionEnter and OnControllerColliderHit in both objects and I'm getting nothing.
Answer by aldonaletto · Dec 26, 2011 at 11:40 PM
Use OnControllerColliderHit in the player script - but be aware that it will report collisions with the ground all the time (except when the player is jumping, of course). To filter out these collisions, you can check the hit.normal angle from the ground - if it's below a low angle (say, 45 degrees) the character collided with other thing than the ground:
function OnControllerColliderHit(hit: ControllerColliderHit){ if (hit.normal.y < 0.707){ // character collided with another thing (not the ground) } }This works because normal.y is numerically equal to the sin of the angle from the ground (it's a normalized vector).
Another alternative is to check if the object hit has a rigidbody, ignoring those that don't have it:
function OnControllerColliderHit(hit: ControllerColliderHit){ if (hit.rigidbody){ // character collided with a rigidbody } }
Whoops - I had put the script in a child of the player, I moved it to the parent and fixed it. You did clarify on which function to use, so thanks!
Your answer
Follow this Question
Related Questions
How to setup character Collisions? 2 Answers
How to properly move a rigidbody/collider? 2 Answers
OnCollisionEnter but have colliding object not move the object it collides with 1 Answer
Preventing objects from intersecting with minimal physics 3 Answers
Objects rotating after falling on another object with Physics & Gravity? 1 Answer