- Home /
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:
The problem starts when I want to have a clowder of cats on screen, some overlapping each other like so:
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.
I'm looking for the same solution. Did you find a good way to achieve this?
Same here, I am looking to a solution to this problem too
How about a static integer, which counts up every time it's accessed. Each cat could then initialise their sprites in the order needed with the current value from the int
I have thought about that as well, the problem is though that you can only set the sorting layer and the sort order of a sprite, and not of the gameobject containing all these different sprites. So this means you have to increase the sort order of -all- underlying children of the gameobject...hmm..... I guess that is possible. I had hoped there would be something more elegant though :) But if none pop up I am gonna try your solution (and hope there is a way to count the underlying number of sprites a game object has so the code wont break if I add another sprite to the game object at a later point). Thanks for pointing me in that direction again!
hexagonius' method was how I ended up doing this. I didn't think it was that great an approach, but my peers couldn't come up with better ones :(
Answer by Basher207 · Apr 13, 2015 at 07:02 PM
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 P.S. 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 :D As far as I know thats the way to do it.
I tried setting the Z-index but in 2D the z-index seems to be ignored. So ins$$anonymous$$d I went for hexagonius method as well and increased the sorting order of each individual sprite to make the compound object as a whole appear in front of another
Answer by bendahmon · Apr 18, 2015 at 08:45 PM
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.