Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Two EdgeColliders2Ds don't trigger collision / don't collide

Discussion in 'Physics' started by svope, Feb 2, 2015.

  1. svope

    svope

    Joined:
    Jul 6, 2014
    Posts:
    8
    Hi guys,

    I'having a problem which I cannot solve, I posted this to Unity answers and moderator rejected that post.

    Problem:
    collision.jpg

    On the picture you can see GREEN road and WHITE ball. Both have edge collider2D component which you can see as those very thin green lines. WHITE ball has a rigidbody2D component ( not Kinematic ) and edge collider2D set as trigger.

    The problem is, those two edge colliders2D don't collide -> there is no OnTriggerEnter2D() or OnCollisionEnter2D() (if I set ball's edge collider as non trigger ) call.

    If I change ONE of those two edge colliders2D to any other 2D collider ( box, circle..) everything works well...

    The code is just standard..
    Code (CSharp):
    1. // this belongs to script attached to the WHITE ball GameObject
    2. // which has EdgeCOllider2D set as Trigger
    3. void OnTriggerEnter2D(Collider2D col)
    4.     {
    5.         Debug.Log( "segsegsege" );
    6.         if ( active_road != col.gameObject )
    7.             col_go.Add(col.gameObject);
    8.     }
    So why two edgeColliders2D don't collide and any other "combination" like edge + box, edge + circle, box + box etc.. works?
     
  2. Uberpete

    Uberpete

    Joined:
    Jan 16, 2014
    Posts:
    78
    Maybe you'd like to look at the documentation: http://docs.unity3d.com/Manual/class-EdgeCollider2D.html

    EdgeColliders are usually used on straight edges, not round stuff like balls. It isn't an "edge" collider per-se but more of a simple surface collider. In the screenshot you posted, the lines are the colliders: the ball's collider isn't in the ball, its just sticking out randomly.

    Unless you really have to, I'd use a polygon/circle collider. Otherwise modify the vertices of the edge colliders.
     
  3. svope

    svope

    Joined:
    Jul 6, 2014
    Posts:
    8
    Uberpete, thank you, BUT THAT IS NOT THE PROBLEM

    Two objects, both have edge colliders, OnTriggerEnter2D() is NOT called
    noCOL.jpg

    THE SAME two objects, where one of them has box collider2D instead of edge collider2D
    and then the OnTriggerEnter2D() is CALLED
    yesCOL.jpg

    I dont care about shapes etc, just why two objects with edgeColliders2D dont call OnTriggerEnter2D (or other collision functions doesnt really matter...).

    For me, it seems like a Unity bug, because as I said, any other combinations of colliders2D WORKS, but ONLY when there two edgeColliders2D then it does NOT work.

    These pictures are from totally new projects, just 2 GameObjects with colliders, one of them with rigidbody and script:
    Code (CSharp):
    1. public class test : MonoBehaviour {
    2.  
    3.     // Use this for initialization
    4.     void Start () {
    5.  
    6.     }
    7.  
    8.     // Update is called once per frame
    9.     void Update () {
    10.  
    11.     }
    12.  
    13.     void OnTriggerEnter2D()
    14.     {
    15.         Debug.Log("TRIGGER");
    16.     }
    17. }
     
  4. Uberpete

    Uberpete

    Joined:
    Jan 16, 2014
    Posts:
    78
    That's because you're calling OnCollisionEnter2D, not OnTriggerEnter2D. Try this:

    Code (CSharp):
    1. void OnCollisionEnter2D (Collision2D col){
    2.  
    3.      Debug.Log ("Hola " + col.gameObject.name);
    4.  
    5. }
     
  5. svope

    svope

    Joined:
    Jul 6, 2014
    Posts:
    8
    I copied a bad piece of code,sorry, but I tried it anyway (written in first post).
    The thing is it just doesn't work.
    You can try yourself if you want ( attachment )
     

    Attached Files:

  6. Uberpete

    Uberpete

    Joined:
    Jan 16, 2014
    Posts:
    78
    I had a look at Box2D manual (Unity's 2D physics engine).

    Its not possible.
     
    Estecka likes this.
  7. svope

    svope

    Joined:
    Jul 6, 2014
    Posts:
    8
    Thank you! Didn't know about this :)
     
  8. schlenger

    schlenger

    Joined:
    Jul 13, 2016
    Posts:
    7
    A bit late, but maybe someone else will stumble upon this. Since EdgeColliders2D are used for sprite shapes, this could be a real usecase. For example you can simulate some fog with a sprite shape and want to test when a player enters it. Here is an example solution for this, in which you need to track the state internally:


    Code (CSharp):
    1. private bool isWithinFog = false;
    2.  
    3. private void OnTriggerEnter2D(Collider2D collision)
    4.     {
    5.         if (collision.gameObject.tag == "Player")
    6.         {
    7.             Debug.Log("enter");
    8.             if (!isWithinFog)
    9.             {
    10.                 Debug.Log("isWithinFog");
    11.                 isWithinFog = true;
    12.                 return;
    13.             }
    14.  
    15.             if (isWithinFog)
    16.             {
    17.                 Debug.Log("left Fog");
    18.                 isWithinFog = false;
    19.                 return;
    20.             }
    21.         }
    22.     }
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    You can use EdgeCollider2D or PolygonCollider2D. If you want a closed shape then use PolygonCollider2D.
     
    KorYme_NakXay and Arik_Cool like this.
  10. Arik_Cool

    Arik_Cool

    Joined:
    Jul 28, 2020
    Posts:
    8
    Thanks This worked! Using wrong colliders can cause no triggers. I used an edge collider for closed object and didn't work until I replaced it with a polygon collider! Thanks A Lot