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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

[Solved] 2D Collision Problems

Discussion in 'Physics' started by Gunging, Sep 7, 2016.

  1. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    I dont know if its because I use sprites, I want to make a prefab destroy itself when colliding with a sprite with tag "Barrier", they have Rigidbody2Ds, Box Collider 2Ds (with IsTrigger true) , same Z and they still dont collide. Can anybody identify what's wrong?

    This is my code if it helps.

    Code (CSharp):
    1.     void OnCollisionEnter2D(Collision2D coll)
    2.     {
    3.         if (coll.gameObject.tag == "Barrier")
    4.         {
    5.             Destroy(gameObject);
    6.         }
    7.     }
     
  2. NowKnown

    NowKnown

    Joined:
    Jul 23, 2015
    Posts:
    5
    check the tag (does the sprite really contain the tag, maybe spelling mistake). You could use Debug.Log("Test"); in OnCollisionEnter to check if there is a collision.

    I'm also new to unity, but I hope this will help!
     
  3. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    No, thats not the problem :c, but thanx

    also the Debug.Log doesnt work, Ive tried, its not detecting the collision, may it be cause they are sprites?
     
  4. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
  5. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    Here's the whole projectile code:

    Code (CSharp):
    1. public class ShootScript : MonoBehaviour
    2. {
    3.    
    4.     public float MoveX;
    5.     public float MoveY;
    6.     public float MoveSpeed;
    7.     Movement player;
    8.  
    9.     void Start()
    10.     {
    11.         player = GameObject.FindWithTag("Player").GetComponent<Movement>();
    12.         MoveX = player.FacingDirectionX;
    13.         MoveY = player.FacingDirectionY;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         GetComponent<Rigidbody2D>().velocity = new Vector2(MoveX * MoveSpeed, MoveY * MoveSpeed);
    19.     }
    20.     void OnCollisionEnter2D(Collision2D coll)
    21.     {
    22.         if (coll.gameObject.tag == "Barrier")
    23.         {
    24.             Destroy(gameObject);
    25.         }
    26.     }
    27. }
     
  6. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    There is no collision because both colliders are set as triggers. Try using OnTriggerEnter2D() instead-- you'll have to change coll from a Collision to a Collider.
     
    Last edited: Sep 7, 2016
    Gunging likes this.
  7. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    So only the barrier should have IsTrigger and the could should be like this?

    Code (CSharp):
    1.  
    2.     void OnTriggerEnter2D(Collider2D coll)
    3.     {
    4.         if (coll.gameObject.tag == "Barrier")
    5.         {
    6.             Destroy(gameObject);
    7.         }
    8.     }
     
    Last edited: Sep 8, 2016
  8. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    :D yay its solved thanks so much guys