Mastering Dynamic Sorting in Unity2D

Nupur MundaNupur Munda
3 min read

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! ๐Ÿ˜Š

0
Subscribe to my newsletter

Read articles from Nupur Munda directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nupur Munda
Nupur Munda

๐Ÿ‘ฉโ€๐Ÿ’ป Hi there, I'm Nupur! By day, I'm a passionate software engineer, crafting solutions in the tech world. But when the sun sets and the code takes a break, you'll find me in my own digital realm as an indie game developer ๐ŸŽฎ. ๐Ÿ•น๏ธ I'm all about bringing pixelated dreams to life and creating immersive gaming experiences. From coding to pixel art, I dive into every aspect of game development in my free time. Join me on this epic journey as I share my insights, stories, and maybe a few lines of code. Let's level up together!