Best way to handle Sprite sorting in compound objects?

Hi all,

I’m currently testing Unity’s 2D features, and am running into a problem with sorting GameObjects made out of several Sprites. Let’s say I have a (very badly drawn) cat Prefab made out of parts, wherein the parts are in the same sorting layer, but in an order that sorts it properly - Tail and legs are order 0, body is order 1, and head is order 2 in the following:

alt text

The problem starts when I want to have a clowder of cats on screen, some overlapping each other like so:

alt text

The head belonging to the background cat hovers over the foreground cat’s body. I want each cat to be in its own discrete “layer” - which could tentatively be done if the order in layer property could be localized to each prefab instance… but I don’t know if it can.

Is there an elegant way to work around this problem?

Thanks for any help.

A simple way to do it is make a script that has a static variable, that starts at 0, for example:

static float catZOffset;

void Start() {
 transform.position += new Vector3(0,0,catZOffset);
 catZOffset += 0.00001f;

but my due its a good idea to have the cats head and body not so far away from each other, like make them into one sprite I think? if you can’t do that because you are changing them individually in your game than I think you can just make there distance to each other closer, and make the catZOffset increment twice as big to make sure that no 2 cats overlap
There might be something with the sprite rendere component that let you do that, and i’m just leading you on to do an overly complicated solution, but idk :smiley: As far as I know thats the way to do it.

My solution to this was to set all objects (in-game stuff that is) on the same sorting layer and have order in layer = 0. Then I used the Z-coordinate to sort them all. This works fine for me. But it is not the way I wanted to do it, as I was hoping that the sorting layer concept was the solution for me.

Maybe I’m wrong but it seems like Unity orders like this: sorting layer, order in layer, z coord. So any object in the same sorting layer and order would be sorted by Z.