Search Unity

Serious bug alert: 2D raycaster sorting 2d colliders based on their world position!

Discussion in 'UGUI & TextMesh Pro' started by xCyborg, Aug 30, 2014.

  1. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    f.gif
    1) What happened
    2D raycaster sorting 2d box colliders and other colliders based on their world position instead of their sorting layers!

    2) How can we reproduce it using the example you attached
    Please look into this I reproduced it in an empty scene with camera containing 2d raycaster and event system components and some 2d box colliders with event triggers.
    Play, and you'll notice the closest sprite to world origin intercepts the ray first irrelevant of its z order!
     
    Last edited: Aug 31, 2014
  2. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Case 630037, this is really serious and very reproducible. I even reproduced it on Unity 5b1. There must alot of guys out there having this issue.
    Up until now I strongly suspect Physics2D.Raycast function since I reproduced the bug from script and apparently 2D Raycaster component calls that function as well. or it could be something with sorting layers even though the rendering order looks fine.
    Do you realize that the whole 2d raycasting system is messed up that way (at least from the z perspective).
    Please UT look into Physics2D.Raycast() if you have done some reimplementation lately.
     
    MrEsquire likes this.
  3. HappyLDE

    HappyLDE

    Joined:
    Apr 16, 2014
    Posts:
    56
    It's not a bug it's a feature.
     
  4. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Try to open this file and watch for the hightlight to kick in at mouse enter, the numbers on the sprites reflect their sorting layer order: Case 630037.zip
     

    Attached Files:

  5. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Thanks for raising a bug, we'll look at it soon.
     
  6. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Unfortunately I'm updated this little project to 5.0.0b10 which has all the UI updates up to 4.6.0b20 but no sign of anything that has been done to remedy that:(
     
  7. MrEsquire

    MrEsquire

    Joined:
    Nov 5, 2013
    Posts:
    2,712
    What about Beta11?
     
  8. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Are we someday gonna take a look at this? it's 4.6.1 and <strike>while GetMouseButtonDown behaves correctly now</strike> it misses some clicks so often, the PointerEnter and PointerExit events are still messed up. Please check the Event Trigger component and maybe enable multi-selection editing too.
    cheers
     
    Last edited: Dec 22, 2014
  9. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    These event-trigger objects are now blocking GUI input when they intersect with UI elements! help:(
     
  10. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
  11. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    Thank you very much phil, I just cloned your fork and compiled and so far PointerEnter and PointerExit events act as expected respecting the 2d layer order! Finally... I have to point however that the GetMouseButtonDown part is still glitchy and missing hits but at least now we got something going.
    Thank you very much again I encourage you all to hit this link: https://bitbucket.org/philip_cosgrave/philunity-uibugfixes/branch/eventsystemrewrite and compare the event interactions after you played with the project I attached above.
    I will take a good look at the diffs you introduced in your fork and get my hands dirty meanwhile you should issue a pull request to UI guys.
     
  12. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    No need for me to issue a PR to the UI guys as I am one of the UI guys :). I just wanted to make sure it worked before putting it into the main branch.
     
  13. bobmoff

    bobmoff

    Joined:
    Sep 23, 2014
    Posts:
    44
  14. bobmoff

    bobmoff

    Joined:
    Sep 23, 2014
    Posts:
    44
    Also tried using 5.0.0f4 with same issue
     
  15. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    I am using 5.1.1p2. And I am seriously confused about how the Physic2DRaycaster picks his collider order. Sometimes it works as described by xCyborg, sometimes not.

    I have some collider with SpriteRenderer and others without. The renderer are in various layers.

    Sometimes it seems to me that the collider without SpriteRenderer are treated as if they were in "Default" sorting layer, but other times they are always hit first. Sometimes the world z-coordinate has an influence and sometimes it seems not to.

    I don't have any GraphicsRaycaster - not even a Canvas.

    The documentation is... well.. y'know..

    Can anyone explain what the order is exactly supposed to be when picking the collider via Physics2DRaycaster?

    By the way, I am not convinced that the behaviour xCyborg talks about is a bug. Why should the collider be sorted after rendering? I would totally expect them to be solely sorted by world z-position and completely ignoring all renderer settings (as opposed to GraphicsRaycaster). That's why its called "Physic" and not "Graphics".

    Edit: Some more testing makes me go crazy. If I start to delete child renderer (with different sorting layers), the order in which the parent collider gets picked changes... Can we have an option to disable render-based picking of physic objects? Or do I have to enroll my own event system to get things dependable again? :(
     
    Last edited: Jul 2, 2015
  16. Carlotes247

    Carlotes247

    Joined:
    Jun 12, 2014
    Posts:
    9
    I agree, is a little bit confusing. I find it easier to use with the Order in Layer field, but it might be a bit out of semantic understanding. There should be an option to choose if you want to use the z position or the order in layer

    However, thank you very much for this thread! Reading it solved all of my doubts regarding how the physics raycaster works.
     
  17. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    You still have problems with it Carlotes? I still don't get it, mind to share?
     
    Last edited: Dec 14, 2015
  18. xCyborg

    xCyborg

    Joined:
    Oct 4, 2010
    Posts:
    633
    I redownloaded the zip, opened the project in Unity 5.3.0f4, the highlights work O.K. (EventTrigger component with PointerEnter and PointerExit), but script raycasting still only sorts colliders in world space, so if my sprites are on the same z-plane it behaves all chaotic and unpredictable.
    So it seems the Physics2DRaycaster (through EventTrigger) uses Order in Layer, but the Physics2D.Raycast function uses world-space inherited from it's 3D counterpart.
    Thanks, everyone. I would only ask for a better documentation to make that all clear. And possibly think of supporting Layer Order in Physics2D.Raycast because it's much more intuitive in 2D that way and we use Layer Order all over in 2D games. And maybe vice versa for world-space in the Physics2DRaycaster.
     
    Last edited: Dec 14, 2015
  19. Carlotes247

    Carlotes247

    Joined:
    Jun 12, 2014
    Posts:
    9
    I was making a very simple event trigger object, but I was confused how the engine was sorting the event triggering when I was having more than 1, as you showed in your example. As you are pointing, the system would need better documentation. However, I find your entire post very useful and it helped me a lot, thanks xCyborg! :)
     
  20. flatbutton

    flatbutton

    Joined:
    Apr 2, 2015
    Posts:
    1
    Hi there,

    I had the same problem and found an OK solution: The Physics2DRaycaster calculates the distance using xyz (see it's current source code here). In my case, I only need the z component. So I made my own CustomPhysics2DRaycaster, which is mostly a copy of the built-in one. I changed the distance calculation from this:

    Code (CSharp):
    1. ...
    2. distance = Vector3.Distance(eventCamera.transform.position, hits[b].transform.position),
    3. ...
    to this:

    Code (CSharp):
    1. ...
    2. distance = Mathf.Abs(eventCamera.transform.position.z - hits[b].transform.position.z),
    3. ...
    Hope this helps.

    Cheers