Search Unity

Character controller collide to destroy gameobject

Discussion in 'Scripting' started by Kimmerss, Dec 6, 2012.

  1. Kimmerss

    Kimmerss

    Joined:
    Dec 6, 2012
    Posts:
    2
    I'm trying to make my first person controller destroy game objects when it collides with them, but none of the scripts i've tried are working, i used OnCollisionEnter just on the gameObject and then I tried using the OnCollisionEnter paired with collision.transform.SendMessage like on the Lynda.com tutorial with no luck on either. Is there something special needed in the code about destroying gameobjects with the first person controller?

    I also tried this:

    function OnControllerColliderHit (hit : ControllerColliderHit)
    {
    if(hit.gameObject.tag == "player")
    {
    Destroy(gameObject);
    }
    }


    Why is it nothing working?
     

    Attached Files:

    Last edited: Dec 6, 2012
  2. Kimmerss

    Kimmerss

    Joined:
    Dec 6, 2012
    Posts:
    2
    Why isnt anyone helping me?
     
  3. sibhod

    sibhod

    Joined:
    Apr 13, 2009
    Posts:
    20
    OnControllerColliderHit is only called on the gameObject with the CharacterController, and only if the controller is moving with the Move() function. Put the collision script on your GO with the CharacterController, and try something like:

    function OnControllerColliderHit (hit : ControllerColliderHit)
    {
    if( hit.gameObject.tag == "gem" )
    hit.gameObject.SendMessage("BeenHit",SendMessageOptions.DontRequireReceiver);
    }

    I'm not completely sure this will work, but hopefully it'll point you in the right direction. Collisions can be tricky to figure out at first, and I still have issues with them. I'd suggest searching Unity Answers or even just google with the keywords of your problem. 99% of the time I've found a solution that way.