2D Sorting

Now that our Player can move around the scene, we may find that they sometimes appear underneath objects they should be in front of:

Unity relies on a sprite's Order In Layer value to sort sprites, which is why we set our background sprites to have negative values earlier.

Sometimes, however, we'll want an object to be dynamically sorted - so that the Player can appear both underneath and above it, depending on their position in the scene. This Tree, for example, should be drawn on top of the Player when they walk behind it.

Solving this problem used to involve AC's Sorting Map component. This is still a valid approach - and may be the one taken in video tutorials - but it's now possible to solve this with Unity itself.

First, we'll make sure that any such dynamic sprites have the same Order In Layer as the Player - in this case, 0:

Next, we need to tell Unity that we want sprites to be sorted vertically - i.e. along the Y-axis. This step varies depending on your render pipeline.

If you're using Unity's built-in render pipeline, go to your Graphics ettings, and set the Transparency Sort Mode to Custom Axis, and then Transparency Sort Axis to (0, 1, 0).

If you're instead using URP, select your 2D Renderer asset, and set its Transparency Sort Mode to Custom Axis, and then Transparency Sort Axis to (0, 1, 0).

Next, we'll set the scene sprite's Sprite Sort Point to Pivot:

This will cause the sprite to be sorted vertically around its origin - the small blue circle visible in the Scene window:

We want this origin/circle to be at the sprite's "ground" position, so that sprites above it on the Y-axis are drawn behind, and those below it are drawn in front. If we need to adjust this position, we can do so by selecting the sprite's asset file and opening the Sprite Editor.

Here, the pivot circle can be dragged into the correct position.

With the proper adjustment, our character will now be sorted correctly relative to other sprites in the scene: