Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

OnCollisionEnter - Destroy object, Simple question

Discussion in 'Scripting' started by UpcomingChris, Jun 3, 2014.

  1. UpcomingChris

    UpcomingChris

    Joined:
    Feb 11, 2011
    Posts:
    201
    This is a simple question, and I've taken a long break from Unity, and I've forgotten alot!, Basically, There's 4 keys around the level, One of which is the "CorrectKey" which you will need to obtain to open a door and progress to the next level, Now. I'm trying to make is so when you walk into the key, It will delete it, and then set a boolean variable to True saying you have a key, The only issue I'm having is i can't seem to get Unity to recognise when I Collide with the key, I don't know if i'm not using the correct colliders or something, But here's a few screenshots.

    Player properties: http://i.gyazo.com/e7bbf7bb25032e04b316de10ca212899.png

    Key Properties: http://i.gyazo.com/9105113e028d874c3aa066ce7416857a.png

    Script (Attached to player): http://i.gyazo.com/1ffe9dbca4e2badb9d0df495c339c52f.png


    Any help is appreciated
     
  2. rcober

    rcober

    Joined:
    Jan 26, 2013
    Posts:
    52
    There are 2 methods related to doing this
    OnTriggerEnter http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
    OnColliderEnter http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

    The issues to getting this working surround whether each entity is a trigger and/or has a rigidbody.
    For collisions, one of the entities must be have rigidbody or is a Trigger.

    For your case, you could check the isTrigger on the key, and handle in OnTriggerEnter.

    The bottom of this page has a useful table:
    http://docs.unity3d.com/430/Documentation/Components/class-CapsuleCollider.html

    For some reason, the new docs got rid of the table.

    In addition, don't forget to check the layers each entity is on - this is controlled by setting the collision matrix.
    (In your case, looks like both are in the Default layer)
    http://docs.unity3d.com/Manual/LayerBasedCollision.html
     
    UpcomingChris likes this.
  3. UpcomingChris

    UpcomingChris

    Joined:
    Feb 11, 2011
    Posts:
    201
    Thanks alot!, Set the key as a trigger and used OnTriggerEnter instead, And it works, Appreciate the help!