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

Ignoring collisions

Discussion in '2D' started by dafuq313, Apr 1, 2016.

  1. dafuq313

    dafuq313

    Joined:
    Mar 29, 2016
    Posts:
    26
    I want to ignore the collisions between the enemies created by "inamici" and points created by"puncte2" .
    This is my script :
    if (Time.time>init+0.5)
    {

    transform.position = new Vector3(transform.position.x, Random.Range(-5, 5), transform.position.z);
    Instantiate(inamici, transform.position, transform.rotation);
    init = Time.time;

    }
    if (Time.time > init2 + 0.1)
    {
    transform.position = new Vector3(transform.position.x, Random.Range(-5, 5), transform.position.z);
    Instantiate(point2, transform.position, transform.rotation);
    init2 = Time.time;
    }
    Physics2D.IgnoreCollision(point2.GetComponent<Collider2D>(), inamici.GetComponent<Collider2D>());



    The problem is that ONLY THE FIRST COLLISION IS IGNORED.The rest of the object created are colliding as usual.Some tips ?
     
  2. sebas321123

    sebas321123

    Joined:
    Mar 19, 2016
    Posts:
    1
    GetComponent<Collider2D>()?

    so GetComponent<BoxCollider2D>(); xd
     
  3. dafuq313

    dafuq313

    Joined:
    Mar 29, 2016
    Posts:
    26
    It doesnt work either.
     
  4. carlself

    carlself

    Joined:
    Feb 5, 2016
    Posts:
    13
    Set "inamici" and "point2" to different layers and edit the Layer Collision Matrix to make the two layers don't collide.
     
  5. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    It sort of looks like you're referencing prefabs in your IgnoreCollision() call. I'm not sure, because I can't see where inamici and point2 are initialized, but try using

    Code (CSharp):
    1. GameObject inm = Instantiate(inamici, transform.position, transform.rotation);     //Replace your first Instantiate() call with this
    2.  
    3. ...
    4.  
    5. GameObject p2 = Instantiate(point2, transform.position, transform.rotation);     //Replace your second instantiate call with this
    6.  
    7. ...
    8.  
    9. Physics2D.IgnoreCollision(p2.GetComponent<Collider2D>(), inm.GetComponent<Collider2D>());     //Replace your IgnoreCollision() call with this
    Instantiate() returns a reference to the created instance, so you need use that reference to specifically ignore collisions with each instance as you create them.

    Also, in the future be sure to use code tags when you post code. http://forum.unity3d.com/threads/using-code-tags-properly.143875/ It allows us to see which language you're using easily as well as reference line numbers, not to mention the color coding.
     
  6. ababab5

    ababab5

    Joined:
    Apr 15, 2014
    Posts:
    508
    Use different layers and uncheck this layers in the Physics2D matrix

    Anthony
    All our games: http://u3d.as/pxE