Search Unity

Collisions with CharacterController

Discussion in 'Scripting' started by bakno, Feb 5, 2008.

  1. bakno

    bakno

    Joined:
    Mar 18, 2007
    Posts:
    604
    Hello

    I have a box collider that I need to destroy when it collides with the CharacterController but not with another objet, let's say a Sphere.

    I use...
    Code (csharp):
    1.  
    2. function OnCollisionEnter (collision : Collision) {
    3.     if (collision.gameObject.name == "MyChar") {
    4.         Destroy (gameObject);
    5.     }
    6.  
    But it does not work. However, if I change "MyChar" for the name of the sphere it works the other way around, destroying itself when colliding with the sphere.

    In other words, how can I make a CharacterController generate the OnCollisionEnter message in a box collider?

    Thanks
    Andres
     
  2. Tubeliar

    Tubeliar

    Joined:
    May 21, 2009
    Posts:
    14
    CharacterControllers act as static colliders, meaning they won't send collision messages to other static colliders. Add a RigidBody component to your box to make it a Rigid Body Collider. It will then receive collision messages from both the sphere and the CharacterController, and you will be able to test which one it was that hit the box. Alternatively, you could add another collider and a rigidbody to the gameobject that has the CharacterController to make them send collision messages to you static box collider.
     
  3. bakno

    bakno

    Joined:
    Mar 18, 2007
    Posts:
    604
    Thanks Tubellar