Unity: 2D translucent effect when a character is “behind” an object
In 2D games, characters (and sprites) move in an x-y plane corresponding to the screen. In order to create a sense of 3D depth, if an object A is “behind” an object B, then A is drawn on the screen before B, so B’s sprite occludes A.
However, if the object A happens to be the player’s character, then the player may not be able to see their character, or may only see a tiny bit of themselves. One method that 2D games (e.g., role-playing games) employ is to create a translucent effect. In this article, I will discuss how to create such a translucent effect in Unity, and provide some sample code you can download to try out!
Why am I writing this guide? When I was working on Mystery Queen, I spent some time searching online for a similar guide. I found various forum posts and discussions on how to do it, and some snippets of code that were useful to varying degrees. However, to get a completely working system, I had to essentially use those ideas and implement it myself. Thus, I figured that having a guide and downloadable content online would be useful for others!
The Unity scenes, assets, and source code for this guide are available for download as a Unity package. You can also access the files on Github. There are separate scenes for each step in this guide, so you can pick and…