- Home /
How to Display UI at a Specific "Depth" (Render Depth) in 3D Space
Hello! I would like to display some World-Space UI elements in front of some 3d objects, but behind others (grouped by layers).
Here's an example (please ignore my terrible drawing skills and just imagine everything to be 3D): If I wanted to display my enemy, Bob the Caterpillar's, name above anything on the Foliage Layer, but behind anything on the Buildings Layer, how would I go about doing that?
I've seen this answer: http://answers.unity3d.com/questions/878667/world-space-canvas-on-top-of-everything.html
Would there be a way to adapt that to suite my needs?
Any help would be greatly appreciated! Thank you!
Answer by ImpOfThePerverse · Sep 27, 2017 at 07:08 PM
Create two cameras for rendering the scene, Foreground Camera and Main Camera. Put objects that go in front of the UI on a Foreground layer.
Foreground Camera: Make the Foreground camera render only the Foreground layer by ticking only Foreground in the culling mask.
Make the foreground camera render on top of the main camera by setting its Depth to a higher number (like -1 for Main and 0 for Foreground).
Make the foreground camera's background transparent by setting "Clear Flags" to "Depth Only".
Main Camera: Set the Main Camera's culling mask up to render everything but the Foreground layer (namely, the Default and UI layers).