0
\$\begingroup\$

I have a simple collision detection program. The red object is controlled by the user and the yellow object is always moving up and down:

enter image description here

However, when the objects are supposed to collide, the red object ends up being underneath the yellow object. I wrote code that should destroy the red object on collision. This is a 2D game project, so I'm not sure how that works. They both have 0 set value for Order In Layer field.

enter image description here

Movement code for red object:

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        //GetKeyDown checks if a button is clicked, but GetKey checks if a button is held down.

        //LEFT
        if (Input.GetKey (KeyCode.LeftArrow))
        {
            transform.Translate(-1 * Vector2.right);
        }

        //RIGHT
        if (Input.GetKey (KeyCode.RightArrow))
        {
            transform.Translate(Vector2.right);
        }

        //UP
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.Translate(Vector2.up);
        }

        //DOWN
        if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.Translate(-1 * Vector2.up);
        }
    }
}

Movement code for yellow object (contains collision event):

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour {

    private bool moveUp;

    // Use this for initialization
    void Start () {
        moveUp = true;
    }

    // Update is called once per frame
    void Update () {

        if(transform.position.y == 30)
        {
            moveUp = false;                               
        }
        else if(transform.position.y == -30)
        {
            moveUp = true;
        }

        if (moveUp) 
        {
            transform.Translate (Vector2.up);
        } 
        else
        {
            transform.Translate (-1 * Vector2.up);
        }
    }

    void OnCollisionEnter (Collision col)
    {
        if(col.gameObject.name == "player_character")
        {
            Destroy(col.gameObject);
        }
    }
}

Also, I'm a complete newbie with Unity, so sorry if I'm asking something that should be obvious.

\$\endgroup\$
1
\$\begingroup\$

For Physics2D the method needs to be called OnCollisionEnter2D and both gameobject needs to have a Collider like BoxCollider2D or CircleCollider2D

public class EnemyMovement : MonoBehaviour 
{
    void OnCollisionEnter2D (Collision2D col)
    {
        if(col.gameObject.name == "player_character")
        {
            Destroy(col.gameObject);
        }
    }
}

For Layer Ordering you are already found the correct property Order In Layer. The red object needs to have a higher number i.e. 1 then the yellow object.

\$\endgroup\$
  • \$\begingroup\$ Same problem. The red object appears below the yellow object and nothing happens. \$\endgroup\$ – But I'm Not A Wrapper Class Jan 4 '15 at 19:18
  • \$\begingroup\$ take a look at my updated answer with colliders and layer ordering. \$\endgroup\$ – JeanLuc Jan 4 '15 at 19:49
  • \$\begingroup\$ Now that puts the red object over the yellow object. \$\endgroup\$ – But I'm Not A Wrapper Class Jan 4 '15 at 21:04
  • \$\begingroup\$ can you debug if OnCollisionEnter2D is called? additionally you need to share screenshots of your hierarchy and the inspector view of both gameobjects \$\endgroup\$ – JeanLuc Jan 4 '15 at 21:15
  • 1
    \$\begingroup\$ One of the objects needs to have a rigidbody2D attached too... \$\endgroup\$ – Savlon Apr 5 '15 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.