- Home /
How do you get an event to call when too Character Controllers hit.
I want a function to run every time two character controllers hit. This function seems to run only when a character controller hits a collider (that is not a character controller. What am I missiing?
void OnControllerColliderHit(ControllerColliderHit hit)
{
if(hit.gameObject.tag == "Player")
{
Debug.Log("HITPLAYER");
}
}
Hi @fantasticmrfox are you sure you tagged both objects Player and both objects have charactercontrolller attached to them
I am sure that both objects have a CharacterController. Tagging isn't the problem, this does not work either. void OnControllerColliderHit(ControllerColliderHit hit) {
Debug.Log("HIT"); }
I don't think I can use OnCollisionEnter, because I am not using rigid body.
Answer by aldonaletto · Jun 28, 2013 at 01:23 AM
As weird as this may sound, the fact is: no collisions are reported between CharacterControllers! The collision system detects such collisions, since the characters don't pass through each other, but no events are generated!
If you want to detect collisions between CharacterControllers, the easiest solution is to use triggers: add a capsule collider to the character and check its Is Trigger field, then add a kinematic rigidbody (a moving trigger needs it to work). The trigger radius must be a little bigger than the CharacterController radius, since this one must penetrate the trigger to a certain extent to be detected (if the Character radius is 0.4, for instance, the trigger radius must be 0.5 or 0.6).
The code could be something like this:
void OnTriggerEnter(Collider other)
{
// avoid spurious collision with itself at Awake:
if (other.transform == transform) return;
if (other.tag == "Player")
{
Debug.Log("HITPLAYER");
}
}