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

Bug Sorting Group not sorting properly?

Discussion in '2D' started by Dadaze, Sep 27, 2022.

  1. Dadaze

    Dadaze

    Joined:
    Sep 21, 2012
    Posts:
    24
    I am migrating my top-down adventure project from Z-depth sorting with custom handling to Y-depth sorting.

    Then I use Sorting Groups for each entity and it's where it becomes weird: some entities sort top to bottom, others sort bottom to top, with the same settings. Even weirder; some sprites are displayed out of order and this bug happens or not when I move other sprites in the hierarchy.

    upload_2022-9-27_10-25-1.png
    As you can see here, I have an eye that is drawn behind the head, which is not supposed to happen, as illustrated by the hoodie that is also placed on top of the head.

    upload_2022-9-27_10-26-43.png
    Then I move the hoodie around in the hierarchy and it's the other eye's turn to be drawn behind.

    I assume it's a bug but there might be some quirks I don't understand. Otherwise, anyone got a good automatized workaround that would not have much of an impact on performance?

    Alternatively, any way to set the sorting method of a sorting group to Z axis? It would be awesome!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    I'm sorry I cannot address the above details specifically because I haven't used the Y-sorting yet, but I offer you this gaggle of random facts about sorting in the hopes that it might trigger something for you to check:

    Three (3) primary ways that Unity draws / stacks / sorts / layers / overlays stuff:

    https://forum.unity.com/threads/orthographic-camera-rendering-order.1114078/#post-7167037

    In short,

    1. The default 3D Renderers draw stuff according to Z depth - distance from camera.

    2. SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties

    3. UI Canvas Renderers draw in linear transform sequence, like a stack of papers

    If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:

    - identify what you are using
    - search online for the combination of things you are doing and how to to achieve what you want.

    There may be more than one solution to try.

    Additional reading in the official docs:

    https://docs.unity3d.com/Manual/2DSorting.html

    And SortingGroups can also be extremely helpful in certain circumstances:

    https://docs.unity3d.com/Manual/class-SortingGroup.html
     
  3. Dadaze

    Dadaze

    Joined:
    Sep 21, 2012
    Posts:
    24
    Thanks for your reply, however my problem is within Sorting Groups.

    Sorting Groups seem to not behave as I would expect. Rendering items in a definite, linear order according to their ChildIndex (position in the gameobjects hierarchy).

    The two pictures depict clear order in the hierarchy and a draw order that seems impossible according to the hierarchy.
     
  4. flasker

    flasker

    Joined:
    Aug 5, 2022
    Posts:
    193
    sorting groups dont sort by hierarchy (nothing sorts by hierarchy in unity)

    they sort by the sprite renderer order in group number

    if all pieces have the same order they will sort randomly
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    This is not accurate. UI objects sort by hierarchy order, and children of a Sorting Group will sort by hierarchy order if the Order In Layer is the same.
     
    Dadaze likes this.
  6. Dadaze

    Dadaze

    Joined:
    Sep 21, 2012
    Posts:
    24
    I assumed that it was the case given the source I used to learn about Sorting Groups. But couldn't find any mention of sorting by hierarchy order (SiblingIndex) in the official doc.