How to Change Sorting Order of Sprites When Swapping Sprite Libraries

Hello!

I’m working on a 2D topdown game that uses bone animations, and I have different Sprite Libraries for the different directions that the character will be facing. Basically I’m trying to figure out how to change the sorting order of the sprites based on the direction the character is facing.

When I press on the left arrow key, the sprite library and bone animation gets changed properly, so that works fine. However, because the sorting orders don’t change, the character looks like a mess!

As an example, the right arm needs the sorting order changed from 17 (in the down-facing direction) to 0 (in the left-facing direction).

I am aware of this method:

mySpriteRenderer.sortingOrder = /*[Sorting Order Number]*/;

However, I believe I would have to use that for every single sprite Category, so I’m trying to avoid that.

As an alternative, I’m trying to create a script that I can assign to the Player prefab that can change the sorting order of multiple sprites based on the direction the character is facing.

I was trying to accomplish this by listing out the names of the Categories in the Sprite Library using enums, but I’m new to Unity and to coding in general, so I’m having trouble figuring this out.

Any help would be appreciated! Thanks so much!

Hey,

There’s no easy one click fix to accomplish this. You have to set the new sorting orders somehow. Only using code for this could be a bit of a pain because it’s difficult to visualize. There are likely more elegant solutions, but I’ll show you how you can accomplish this with a mix of code and animation. This is just to give you an idea of what is possible. I haven’t actually tested any of it, so take it with a grain of salt.

The idea is that you create a second animator layer to control the sorting order for each sprite. A script will tell the animator which direction the character is facing, and then the animator plays an animation on the second layer that overrides only the sorting order of the sprites.

This is the script:

    enum Direction { down, up, left, right } // This just makes what int equals what direction easier to understand.

    Direction currentDirection = 0; // This is the current direction the player is facing - you need to set this somehow.

    Animator anim;

    void Awake()
    {
        anim = GetComponent<Animator>();
    }

    void Update()
    {
        anim.SetInteger("Direction", (int)currentDirection);
    }

You need to set the “currentDirection” variable yourself, and then it updates the “Direction” parameter in the animator so the animator knows where you’re facing.

Animate the sorting order for each direction:

Animating the order will make it easy to see what sorting order you need for each sprite for each direction. In my examples, I’m using four directions, but you just need to make an animation for each direction your character can face.

Then, create a new animator layer to control your sorting orders depending on the direction the character is facing:

Make sure the layer weight is set to 1.

Create the “Direction” int parameter.

Create conditions for each transition:

You’ll likely want to change some of the other settings as well, like unchecking “Can Transition To Self” if you’re transitioning from Any State.

That’s pretty much it. If all of this is too confusing or convoluted, please feel free to ignore it haha.

If you have any questions, feel free to ask. Good luck!

1 Like

you will indeed have to use
mySpriteRenderer.sortingOrder = /*[Sorting Order Number]*/;

you will have to do it for every direction manually

its not that much work, you only have to do it once for each direction

1 Like

Thank you so much for your reply! It looks like I had the directions set up okay, but I didn’t realize I could just add the sorting order as a property to the animations. All I needed to do was add that and it works! Thank you so so much for your help with this!!

1 Like