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.

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…