When building on 2D games with Unity3D, one common issue that we will be dealing with is the sorting the layer of the sprites. In the common way, we can use Sorting Layer and Order in layer to solve most of the 2D game sorting issues.
You can find the sorting layer and order in the Sprite Renderer component of your sprite. By clicking the sorting layer drop down, you can assign the sprite to any layer such as foreground, mid-ground, background and etc. If you want to create or delete the layer, you may select Add Sorting Layer which will bring you to the Tag and Layer window to edit modify your sorting layer.
In some cases, you may want to sort different sprites within the same layer, for example, a grass sprite in front of a rock sprite. In this case, you can use Order in layer to sort the sprites within the same layer, the sprite that has higher number of the order will render in the front.
Sorting Group For Nested Sprites
Sometimes, you may have a nested sprites which contains different part of an object, for example, head, legs, body and etc. If you have more than 1 nested sprites in the same sorting layer, you may have some issues when the nested sprites is overlapping with each other, just like the cute T-Rexes below:
You will notice that when the green T-Rex is overlapping with another T-Rex, the head and the leg of 2 T-Rex is not sorting in the right way, they just look like dislocated from the body. To fix this issue, we will need to use Sorting Group for all the nested sprites.
By adding the Sorting group components in the T-Rex, all the child sprites will be sort accordingly with the correct order like a single sprite. If we try to move them around, they will just overlapping each other as a whole.
If you are creating 2D platformer game using Unity 3D, using sorting layer and sorting group will help you sort different sprites in the correct order. That's all for today! Hope this tutorial help you and have a nice day!
If you are developing a 2D top down game, we would like to recommend you to read our tutorial about Transparency Sort Axis and Transparency Sort Mode