- Home /
How to make a 2D sprite always render in front of the 3D gameobjects
For a game I am making, I need 2D sprites to always render in front of the other 3D GameObjects in the scene (not the UI).
I have tried using sorting layers, but they don't seem to work in this case. It may be because I'm working with 3D objects, and I could not a way to set a sorting layer for a 3D GameObject.
Normal layers aren't working either, no matter what I set it to, even UI, the sprites will still render based on their distance from the camera.
Answer by BorpaBoatski · Jun 18, 2021 at 01:02 AM
What I did to achieve that was to make a copy of the Default Sprite shader code and added a ZTest Always to the SubShader.
You can download a ZIP of built in shaders at the Download Archive.
The DefaultSprite shader is located in the DefaultResourcesExtra folder. Then you'll want to add the ZTest Always line which would now look something like this.
Cull Off
Lighting Off
ZWrite Off
ZTest Always
Blend One OneMinusSrcAlpha
Now apply that new shader onto the sprites you want to always render in front of 3D objects
Thank you this is a great solution.
However for me, I came up with a different idea that will make the UI of my game better and does not need this.
I'm sure many other people will use this though.
Your answer
Follow this Question
Related Questions
Sprites keep overlapping between game objects 1 Answer
Z order for GameObject 0 Answers
is there any way to group sprites? - Unity2D, 1 Answer
Sorting Layers in a 3D Environment 1 Answer
Per-Sorting Layer Collision 1 Answer