Search Unity

Inconsistent behavior when clicking on overlapping objects

Discussion in '2D' started by fealtydev, May 1, 2019.

  1. fealtydev

    fealtydev

    Joined:
    Dec 25, 2018
    Posts:
    16
    Hey folks,

    I have two game objects ForegroundObject and BackgroundObject, with SpriteRenderers and BoxCollider2D, that occassionally will overlap. I am seeing inconsistent behavior with picking which thing was actually clicked on.

    I am detecting clicks by implementing IPointerClickHandler and IPointerDownHandler, and I have ForegroundObject and BackgroundObject in different sorting layers. I also update the sorting order of the sprites based on their y position as they move.

    What am I doing wrong that leads to this inconsistent picking behavior? I would like to avoid explicit raycasting for a few reasons, but if that is the only way...

    If I can add anything to this post to further clarify please let me know. Thank you
     
  2. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    78
    Can you better describe what you mean by inconsistent behavior? Maybe post the relevant code. The IPointerClick and Down interfaces should be sending their messages to everything that was clicked on. I believe the order in which they're processed is undefined, but you could experiment by changing the z depth of each object and see if that stabilizes. Most likely your inconsistent behavior stems from how you're processing the events.
     
  3. fealtydev

    fealtydev

    Joined:
    Dec 25, 2018
    Posts:
    16
    Wow, sorry for leaving out the description of what's actually happening.

    What I am experiencing is that sometimes the foreground object responds and sometimes the background object does.

    When clicked I call a method on a singleton. It seems that it's never that both objects make the call, or trigger OnMouseDown, it's one or the other.

    I will test with Z value, thank you.
     
  4. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    65
    Unfortunately, there is currently a major limitation within Unity in 2D with regards to overlapping object colliders on the same Z axis (for pointer detection).

    It will *always* randomise which object receives the click, regardless of whether one is above the other in the sorting layer. I did raise it as a bug request/feedback, but I didn't get any responses from Unity so it looks like we're stuck with it.

    The problem with changing the Z axis is you need to then keep recalculating it & changing it every frame - on every object - since objects move around each other and will sometimes need to be in front or behind other objects from one moment to the next.

    To be honest, it still amazes me that Unity has such a massive limitation like this for 2D games. You can get around it with firing RayCastAll every mouse click and then iterating over the results to find the object which is on top, but it's a real hassle.
     
  5. fealtydev

    fealtydev

    Joined:
    Dec 25, 2018
    Posts:
    16
    This is very helpful. Fortunately for me, the Background objects do not move.

    Thank you both for your help.