You may want to read our previous blog about Sorting Layer and Sorting Group before continue reading this.
If you are developing for 2D top down games where the sprites will move from top to bottom or vice versa, sorting layer and sorting group may not enough for you. You will probably face some problem when one of your sprite is moving from top to bottom of another sprite.
When the green T-Rex is moving from the bottom to the top of brown T-Rex, it's suppose to render behind brown T-Rex but it does not. Luckily, Unity 3D provided a very convenient settings to solve this problem, it's called the Transparency Sort Mode and Transparency Sort Axis.
These settings can be found from Edit > Project Settings > Graphics. By changing the Transparency Sort Mode to Custom and Transparency Sort Axis's Y-axis to 1, it's tell Unity3D to render the sprites with higher Y position at the back of screen. Now we can see the green T-Rex will render at the back of the brown T-Rex when we move it up.
That's all for today! See you guys again.
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