- Home /
Always render a object on top of another.
Is it possible to make a object always render on top of another even if there is another object between it and the camera. Like making the gun in a FPS always render on top even if it goes trough a wall.
Im preferably looking for a way to do this with the standard version of unity, and not use any pro-only features.
Answer by spinaljack · May 22, 2010 at 01:27 AM
Best solution is to place a second camera on the scene and put the gun in front of the other camera, set the gun to a layer of it's own and make the second camera only render the gun layer, set the camera to a higher depth than the main camera so that the gun renders over the main view and set it to depth cull so that it doesn't completely hide the main camera.
The result is that the gun will always appear above everything else. If you wanted to have the gun get shaded by lights near the player you can place the second camera as a child of the main camera so that the gun and camera move around with the player.
This is also how I render special HUD elements like way point arrows and $$anonymous$$imaps.
Hi there,
I am trying to set this up but my secondary camera seems to be covering the main one. As far as I can see I have set it to only render one layer and also it has culling on to $$anonymous$$imum distance. Have you any idea what I am doing wrong?
The second camera must be a child of the main camera and must have "mouse look" too.
On the Second Camera:
Clear Flags = Depth Only
Culling $$anonymous$$ask = "New Layer Only"
Depth = 1
The rest as default
On the $$anonymous$$ain Camera:
Culling $$anonymous$$ask = Everything except the new layer
...perhaps this can help you.
Also looks like you need to select Clear Flags: Don't Clear
for child camera
Will you still receive dynamic shadows with this method? I have never gotten it to work with two cameras.
yes you will receive dyanamic shadows as your layer is default and should provide shadows of the object in the scene.
There has to be some way to create a special rendering order, one layer before another. That's how they programmed it, shouldn't we have access to it being, you know, developers?
Answer by osamansr2o1oo · Sep 20, 2018 at 10:33 AM
in case anybody didn't get the accepted answer !
***Edyvargas simplified it in a comment on the accepted answer check his comment***
Clear Flags = Depth Only
Culling Mask = "New Layer Only"
Depth = 1
On the Main Camera: Culling Mask = Everything except the new layer
Answer by Mr-Jun · Jan 30, 2017 at 10:48 AM
This idea looked intimidating at first, but its actually super simple. Thanks for the answer. Yes, still usefull in 2017 :)
How can this be applied to a S$$anonymous$$mVR CameraRig?
Answer by ChrisRede · Feb 04, 2020 at 06:41 PM
Works also on 2019.1.0f2 and with VR! Nice Thanks!
How did you make it work in VR? I did exactly as the instructions above, but my object in its dedicated layer isn't rendering.