Search Unity

Cannot detect collision in a 2D environment

Discussion in 'Scripting' started by jmaldonado93, Apr 20, 2014.

  1. jmaldonado93

    jmaldonado93

    Joined:
    Oct 19, 2013
    Posts:
    89
    I have read several posts about collision detection but I have not been able to make it work in my case which I describe below.
    My game is 2D with the following characteristics:

    * A main spaceship game object with Rigidbody 2D and Polygon Collider 2D components added. No gravity.
    * Several prefab asteroids that I instantiate at run time. They have Rigidbody 2D and Polygon Collider 2D components added. No gravity.

    There is no gravity because spaceship and asteroids move horizontally.
    A script is attached to the main spaceship to move it up and down to avoid the asteroids. This same script includes OnCollisionEnter to detect collisions but it seems nothing happens when the spaceship and an asteroid contact each other. For now, all I do in OnCollisionEnter is debug.log a message.

    Another script is attached to the asteroids to move them towards the main spaceship.
    Below is the code to move the spaceship and which is supposed to detect collisions.
    Any help will be very much appreciated.

    Regards,
    Jorge Maldonado

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MoveSpaceship : MonoBehaviour {
    6.     public float fltSpeed = 4;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.     }
    11.  
    12.     //***************************************************************************************
    13.     // Update is called once per frame.
    14.     //***************************************************************************************
    15.     void Update () {   
    16.         Move ();
    17.     }
    18.  
    19.     //***************************************************************************************
    20.     // Move Spaceship Up and Down.
    21.     //***************************************************************************************
    22.     void Move()
    23.     {
    24.         if (Input.GetKey (KeyCode.UpArrow))
    25.         {
    26.             transform.position += new Vector3 (0.0f, fltSpeed * Time.deltaTime, 0.0f);
    27.             if (transform.position.y > 4.5)
    28.             {
    29.                 transform.position = new Vector3 (transform.position.x, 4.5f, 0.0f);
    30.             }
    31.         }
    32.         else if (Input.GetKey (KeyCode.DownArrow))
    33.         {
    34.             transform.position -= new Vector3 (0.0f, fltSpeed * Time.deltaTime, 0.0f);
    35.             if (transform.position.y < -4.5)
    36.             {
    37.                 transform.position = new Vector3 (transform.position.x, -4.5f, 0.0f);
    38.             }
    39.         }
    40.     }
    41.  
    42.     void OnCollisionEnter(Collision collision) {
    43.         Debug.Log("Collision detected !!!");
    44.     }
    45. }
    46.  
     
  2. Felandil

    Felandil

    Joined:
    Jul 30, 2013
    Posts:
    32
    try OnCollisionEnter2D
     
  3. jmaldonado93

    jmaldonado93

    Joined:
    Oct 19, 2013
    Posts:
    89
    It´s working. Thank you.