Cultivate: Before Time Dev Blog #4 – 2D Sorting order and Sprite Fading

So that the character can move behind objects like trees, I’ve set up a system that orders everything correctly on the screen using Unity’s sprite sorting order. Objects that are higher should be behind objects that are lower down.


I’ve added colliders to each tree as well as to the character to stop the character walking through the trunk, and the trees become semi-transparent when the character goes behind them.




So how does this work?


Each object that’s an obstacle has an Obstacle script attached to it that contains the code to make it semi-transparent and implements the IComparable interface to define how it should be ordered. Each obstacle also sets its own sorting order based on its position in the game world.


The character’s child gameobject has a trigger collider component and a script that checks if it collides with an obstacle. If it does, the obstacle fades out so that the character can be seen beneath it, and the character’s sorting order is set based on the sorting order of the object it’s going behind. When the character moves away, the object fades back in.


Bookmark the permalink.

One Comment

  1. This is a really cool effect! A subtle thing that makes such a difference, because it ads so much depth and appeal. Nicely done.

Leave a Reply