Using sprite renderer "order in layer" for depth... Help needed :(

Hi, I’m creating a game which generates a large map for the player to explore. The map is made up of lots and lots of sprites. They are terrain sprites and prop sprites like trees. Each of the sprites has a variation of the following code attached which sets the layer of the sprite renderer based on the y position:

void Start ()
    {
        gameObject.GetComponent<SpriteRenderer> ().sortingOrder = (int)gameObject.transform.position.y * -1;
    }

The script works find in regards to setting the layer position.

The problem I’m having is with the Character.
What I’m trying to do is make it possible for my character to run in front of and behind trees and other objects. Currently the character is made up of multiple sprites and each of them have their own layer in a separate character order layer as shown in the image bellow:

I need to implement something similar in Update() for the player which adjusts the layer based on the y position but it needs to take in to consideration all of the sprites and layers in the character object.

I have considered changing the z position instead but I would really rather not.

Does any one have any ideas how I could go about getting this done?

Many thanks!

The new SortingGroup component added in 5.6 is what you want. Put it on the parent, and change the values on the parent to set the sorting for the entire character hierarchy as one object.

4 Likes

THANK YOU! This helped me today. I was setting the sorting order manually, but it still wasnt working.

TY, TY, TY

1 Like