- Home /
Enemy Position
Hello i was wondering if anyone could direct me to a link or help me on how to create a GUI button that floats right above the enemys head. Thanks in advance!
Answer by AlucardJay · Sep 05, 2012 at 05:14 AM
There are 2 methods you could use. One I don't recommend is drawing a GUITexture aligned to the player/character. Another method is the 'floating health bar'. Create a plane above the character as a child of that character. Write a very short script that tells the plane to face the camera, or as here rotate to the same angle as the camera =]
http://answers.unity3d.com/questions/195711/minion-floating-healthbar-over-the-head.html
http://answers.unity3d.com/questions/20697/how-to-create-billboarding-planes.html
http://forum.unity3d.com/threads/26014-Health-bar-over-enemy
https://www.google.com/search?q=unity+floating+health+bar
I had to fix your title, it was bugging me =] )*
lololol thanks ill take a look at it tommorrow im to tired right now to take a look at it. Thanks i really appericate the help :)
Yep, that's a great option, too. It also has the advantage of making the health bar's size change depending on how far you are away from the enemy.
Answer by Screenhog · Sep 04, 2012 at 11:42 PM
I'd start here:
http://docs.unity3d.com/Documentation/ScriptReference/Camera.WorldToScreenPoint.html
Now, you just need a Vector3 point where you want the GUI button to float.
ohh okay so would this work
using UnityEngine; using System.Collections;
public class example : $$anonymous$$onoBehaviour {
public Transform target;
target = GameObject.Find("Orge");
void OnGUI(){
Vector3 screenPos =
camera.WorldToScreenPoint(target.position);
if (GUI.Button(new Rect(target.postion), "Attack")){
(Code goes here) } }
No, because your Rect needs to be four floats. Target.position merely tells you where the center of the button needs to be. You need to define the left point, top point, width, and height of the button based on this.
ohh okay thanks so if i wanted it to be floating above the enemys head how would i code the left point, and top point
The point you know is the center of your rectangle, right? So, your left and top point will be relative to that.
Let's say you want your Rect to be 100 pixels wide. If screenPos = camera.WorldToScreenPoint(target.position), then screenPos.x-50 would be your left point (50 because it's 100 divided by 2). Your Rect will look something like this:
Rect(screenPos.x-50, screenPos.y-30, 100, 60)
Your answer
Follow this Question
Related Questions
Deduct health on collision 2 Answers
String not showing up in GUI label 1 Answer
C# Floating Health Bar 2 Answers
How To Make A Floating Health Bar 1 Answer
GUI follow enemy object 1 Answer