- Home /
Collisions with a character controller
Hello everyone.
I'm trying to make a simple sidescrolling shooter game.
The main character is controlled with a character controller and the enemy's bullets are in a box trigger collider. That's the code I'm using, wich is placed in the bullets:
function OnTriggerStay (other : Collider) {
if(other.gameObject.CompareTag("Player")){Destroy(other.gameObject);}
}
The script seems to work fine, but only when the main character is moving, when he's staying still, the collision doesn't seem to work fine. What I am doing wrong?
Thank you
Well, Seems that attaching a kinematic rigidbody to the bullet works. Does anyone know why?
If you look up the Unit Collision $$anonymous$$atrix, it describes how various things hit/don'tHit each other. The short version is that if you tell Unity that two things won't move (like neither has an RB,) it won't waste time checking if they do, even if you do move them.
The funny thing is, CharacterControllers aren't in that grid. They're a weird beast that inherits from Collider but doesn't quite act like it -- even have their own OnCharControllerHit.
Answer by Peter G · Oct 23, 2011 at 05:25 PM
As Owen says, the collision only happens because the Character Controller "ran into" the static bullet collider. A collider without a rigid body, Unity assumes is static. So Unity doesn't check to see if it moves into another collider.
But Unity still tests to see if any objects have moved into it hence it does detect the character controller moves into the trigger. The easiest solution is to put a kinematic rigid body on the bullet.