Hi,
I have a problem with the collision detection of the CharacterController component.
I created a small bomb object and i want it to explode when the character touches it. The player object has a character controller object attached to it, while thethe bomb has a rigidbody and a capsule collider. As long as the bomb is moving, the method OnCollisionEnter works fine for collision detection, but as soon as it stops, I can not detect collisions anymore. I tried OnControllerColliderHit, too, but this doesn’t even work for a moving bomb.
Is there a better method for the detection of collisions?
Collisions between rigidbodies and CharacterControllers are weird: OnCollision events are generated when the rigidbody hits the character, but not the other way around (except in some rare cases). These are some ways to work around this:
1- Use OnControllerColliderHit in the character script (this event isn’t sent to the other object):
function OnControllerColliderHit(hit: ControllerColliderHit){
if (hit.tag == "Bomb"){
hit.SendMessageUpwards("Explode"); // call the function Explode in the bomb script
}
}
2- Child a somewhat larger trigger volume to the bomb, and use OnTriggerEnter to explode it (trigger script):
function OnTriggerEnter(other: Collider){
if (other.tag == "Player"){
SendMessageUpwards("Explode");
}
}
The first alternative is easier, but is tied to the player. The second solution requires you to child a trigger volume (say, a sphere with the mesh renderer disabled) to the bomb, but doesn’t need any changes in the player.