- Home /
Show GUI only when looking at an object?
I have a script with a GUI health bar attatched to an object, that changes when hit by rockets. Is it possible that the player can see that GUI only when looking at said object, and no other time? I've tried using Raycasting, but that only checks the shortest path between the player and the object, not the players line of sight.
no it doesnt only check the shortest path. it only does that if you cast from player to object. Cast the right direction.
If its fps cast through the crosshair in a sphere or do a field of view check on the camera to see if its in the field of view. depending on how precise you want it to be.
ray.origin = player.transform.position;
ray.direction = player.transfrom.forward;
is the easiest way
Answer by Itinerant · Nov 19, 2012 at 07:55 PM
I'd suggest using the Renderer.isVisible attribute. Attach it to the object and tie that into the GUI. Documentation is here: http://docs.unity3d.com/Documentation/ScriptReference/Renderer-isVisible.html
Remember to mark answers as correct if they're what you needed. $$anonymous$$eeps everything clean :)