- Home /
Floating Enemy Health Bars?
Hey how would I go about making a floating enemy health bar?
I only want it to work if you are aiming at the player. (This is for an online game).
Which the Networking part isn't the part I need help with.
Just would like to know how to make it look like in the image (Just appear under the player) if you aim at him.
Such as the one in the image.
(I'm not familiar with this new UI) so I'm kind of lost. Right now I have a World Space Canvas just on my enemy, if an object runs into it, it gets covered up and is always on.
Answer by Cherno · Jan 07, 2015 at 02:24 AM
In simple terms:
Raycast from the Camera forward, and store the transform.position of the enemy object if the ray hits one. If you need to access any variables on the enemy object, store these as well (or store the whole object)
Convert the enemy object's position into screen space coordinates.
Use these coordinates to draw the health bar on screen. For the specific health value, access the necessary variables from 1).
How do I convert a vector3 position into screen space coordinates? And this is for the new UI?
(Sorry for the stupid questions LOL).
You use Camera.WorldToScreenPoint to convert the coordinates. If this is for the new UI I can not answer since I haven't used it myself, but I assume that it needs the screen coordinates to know where to draw the health bar texture :)
Try creating your health bar, and storing it above the players head, with a collider of some sort, child it to the player and it will follow him around. Setup a raycast function to players collider. You can setup a function in your script to change the alpha of the texture to 0 unless a raycast hits the collider of the player, then it will set the alpha back to normal so the health bar appears. You can also add a fade in fade out effect after the ai$$anonymous$$g is complete or begun. Im new ot scripting also, but im pretty sure this is the simple way to go about it. If i am wrong someone will tell me. Happy hunting.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Create big city for car game? 2 Answers
Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers
Gameover function calling before game ends help 2 Answers
GUI.Window Problem Option Menu 1 Answer