Display a gameObject like a GUITexture

Hello everyone !

I’m having a hard time with a life bar in a multiplayer FPS game. I created a gameobject like that (so a gameobject with a mesh filter and a mesh renderer). Every character has two cameras : one for the general display of the level and other players, and another one for the GUI display (remaining ammo for example). I want to display my life bar with the second camera.

My problem is that even if I gave the same layer to my life bar as my GUIText gameobjects for ammo, the life bar is drawn, but can go threw walls (like it is not drawn in last, contrary to all other “GUI” gameobjects.

My “GUI” camera already has a depth superior to the main camera, and if I remove the mesh filter and mesh renderer from my life bar and replace them by a GUIText, it works fine …

This problem is driving me crazy :frowning:
Thanks by advance !

Ah in that case, it has to do with your camera being positioned either too far behind the player that it goes through objects, or improper colliders.

Perhaps, try using a Raycast that will detect if the cameras ‘back’ is x distance from the wall, and stop it from moving once it has reached this point.

Your problem is that a regular GameObject has no knowledge about that it’s supposed to be a “GUI” object. You will need to create and assign a shader to it that ignores any existing z-Buffer values. As a result, the object will always be rendered, independent from its actual depth, and independent form other scene objects.

Unless you already have a suitable shader, download the builtin shaders from Unity - Manual: Built-in shader variables , find the “Unlit-Normal.shader” if your bar does not use alpha/transparency, or “Unlit-Alpha.shader” if it does. Copy it into your project, edit it, give it a new name (for example, adding “(GUI)” to its name), and add this line directly before the Pass section:

ZTest Always

EDIT: with this approach, you don’t need a separate camera to render your GUI GameObjects, you can just place them as children to your regular camera, and adjust their local coordinates accordingly. On the other hand, for your two-camera-approach, a special shader shouldn’t be required. Make sure your GUI camera has a higher “depth” setting, and the “clear flags” are set to “Depth only”.