Z position issues in a 2D Zelda-style game

I’m working on a 2D game that uses a 3/4 perspective like Zelda. Each of the characters is comprised of multiple sprites-- head, arms, body, legs, hair-- all nested under the gameobject “CharacterSprites”, and each having a unique Z position in relation to the other body parts, so that hair appears in front of head, arms appear in front of body, etc. I set the overall CharacterSprites Z position based on the character’s Y position, so a higher Y position results in a Z position that is further back, allowing one character to stand behind another when they overlap.

This works well until I have two overlapping characters at exactly the same Y position. This can result in one character’s hair appearing in front of the other character’s face, and other z-ordering mixups. Is there a better method of handling this sort of thing within Unity?

This is not great, but you could set each sprite renderer’s “order in layer” property dynamically based on the Y coordinates, and reserve Z coordinate changes for sorting within a character. I don’t know the performance implications though, and “order in layer” is an integer field so unless your Y values are already quantized you may need to sort your sprites by Y first in order to give each a sensible index.