Welcome back, fellow game developers! In the previous episode of my game development journey, we dived into the fascinating world of crafting unique game assets. Today, we're going to tackle a rather pesky issue I encountered in my game – sorting.
In case you missed my last blog post, you can catch up here: Crafting Unique Game Assets
The Sorting Conundrum
In the earlier version of my game, I stumbled upon an issue that's quite common in 2D game development – the sorting of sprites. If you've ever created a 2D game, you might have encountered situations where certain elements appear behind objects they should logically be in front of.
For instance, in my game, the vampire's head was mysteriously hiding behind the roots of a tree, which, as you can imagine, looked quite absurd. Here's how it looked
Unraveling the Mystery
The mystery of this weird sorting behavior can be attributed to how Unity handles sprite rendering. By default, Unity considers the center of the sprite as the reference point for sorting. This can lead to bizarre situations like our vampire's head disappearing behind tree roots.
So, how did I solve this? It's a two-step process:
Step 1: Adjusting the Sprite Pivot Point
First, I needed to change the pivot point of the sprite from the center to the bottom. This simple adjustment tells Unity where the sprite should be anchored. In this case, I wanted the sprite to be anchored at the bottom, so it would appear in front of objects logically.
Step 2: Customizing Sorting Layers
The second step was to customize the sorting layers in Unity. This is where the magic happens. By specifying sorting layers and orders, you can precisely control the rendering order of objects in your game.
In my case, I set the sorting order to be something like this:
X: 0
Y: 1
Z: 0
This ensured that the vampire's head would be rendered in front of everything else, including those tree roots that used to swallow it up.
Under Project setting> Graphics
The Difference It Makes
The results were astonishing! With these simple changes, my game transformed from looking like a quirky art project to a polished and professional 2D adventure.
Wrapping Up
Game development is full of challenges, but it's also about discovering creative solutions. Sorting issues in Unity2D can be frustrating, but with the right techniques, you can achieve the visual results you desire.
I hope this blog post helps you tackle your own sorting challenges in Unity2D. And, as always, don't forget to play my game and share your valuable suggestions for improvement. Together, we can make gaming experiences even more fantastic!
Until next time, happy coding and happy gaming!
PS: Play my game here and share your suggestions! 😊