Sorting Group not sorting properly?

,

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.


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.


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!

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:

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:

And SortingGroups can also be extremely helpful in certain circumstances:

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.

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

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.

1 Like

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.