- Home /
The question is answered, right answer was accepted
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 :( Thanks by advance !
Do you mean using a 3D object as if it were a GUI texture? You could just make the the gameObject a child of the Camera component, position it correctly, and it should always follow the camera at the set position .
Did you specify the Culling $$anonymous$$ask for each Camera?
@$$anonymous$$ontraydavis : yes, as if it were a GUITexture. It is already a child of my camera, but my problem is that it goes threw walls if my character is against a wall.
@PAEvenson : yes, I can see as I want my game objects. The main camera has every culling masks BUT one specific ton my "GUI" things, and the other camera has only the specific mask.
Answer by Wolfram · Nov 13, 2012 at 01:34 PM
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 http://unity3d.com/support/resources/assets/built-in-shaders , 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".
Actually I am already using a cutout vertex-lit shader on it to handle an effect. Can we combine shaders ?
Same approach, make a copy of the shader, give it a different name, and add the line above. In your case, the filename of the builtin shader is AlphaTest-VertexLit.shader
Strumpy Shade Editor can achieve this too. : http://forum.unity3d.com/threads/56180-Strumpy-Shader-Editor-4.0a-$$anonymous$$assive-Improvements
@$$anonymous$$ontraydavis: that's a powerful tool, but a bit of an overkill for the problem at hand ;-)
@Wolfram: True true . I was thinking he/she could learn from Strumpy though, and maybe just spiff out this specific part of strumpy, and figure it how strumpy does it lOl. But yes, it is a bit of overkill lOl.
Answer by Montraydavis · Nov 13, 2012 at 01:14 PM
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.
http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html
$$anonymous$$y camera does not go threw walls. It is only my "life bar" game object that goes threw. Text meshes game objects that have the same culling mask are rightly displayed and don't go threw walls...
I was careful to put my camera inside my collision capsule :)
Well, I'm thinking it's basically the same issue, except with the texture and not the camera.
The only way I can really think to solve this is by using Raycasts, as I mentioned before, but on the object that's going through walls, and have it raise above the character while he is on the wall, or either in front / side of him. The problem is definitely the positioned of the object interfering with the position of the player.
Ok, so a simple possibility is to put the game object into the capsule collider, and reduce the scale to have what I want ?
Exactly. You need the gameObject to be close enough so that it doesn't go into the wall when you're x distance away from it. But like I said, alternatively, you can just re-position it when you're against the wall. Either approach should work.
Try it out, and mark as answered if this helps! Thanks for using Unity3D .