Health Bar above ememy

I can make a health bar show up at a static place at the a predefined top and left but I want it to show up above my enemy.

I added the script to my enemy, but I can’t figure out how to get my enemy’s top and left on the screen to put the health bar above him.

Can someone help?

Thanks!

Yea you can use the camera to convert the players position to screen coordinates and then use that to calculate your Rect for GUI.

so it would look something like this ( do check if Camera.current is non null )

     Vector3 screenPosition =
           Camera.current.WorldToScreenPoint(transform.position);// gets screen position.
     screenPosition.y = Screen.height - (screenPosition.y + 1);// inverts y
     Rect rect = new Rect(screenPosition.x - 50,
           screenPosition.y - 12, 100, 24);// makes a rect centered at the player ( 100x24 )
     GUI.Box(rect, "Enemy");

Thats how you would go about getting the rect anyways.

That works, but creates additional health bars on the other side of the view. When my back it to the enemy, there is also another health bar hovering over nothing.

Somehow I need to check to see if the enemy is in sight.

Try something like this:

private var draw : boolean=true;
function OnDrawGUI(){
 if (!draw) return;
 // > Healthbar code<
}
function OnBecameVisible () {
 draw= true;
}

function OnBecameInvisible () {
 draw= false;
}

Worked great! Thanks

thank you, it works great

I have the same problem with additional health bars on the other side of the view. Anyone knows how to solve it please?

try this script

Great Topic! Great Ideas!!! Helped a lot!!!

THANKS!!!

JasonWIR show me your code please, i have tried this, but it doesn’t worked for me.

Modified the code to show a different kind of health bar, and then some text indicating the characters HP. Also I added a raycast so if a character is behind an object his HP wont show up. The script just needs to be attached to the character you want to have a health bar, and maybe some other minor changes in the script tailored to your game.

var health : float;
var maxHealth : float;

var adjustment : float= 2.3f;
private var worldPosition : Vector3= new Vector3();
private var screenPosition : Vector3= new Vector3();
private var myTransform : Transform;
private var myCamera : Camera;
private var healthBarHeight : int= 5;
private var healthBarLeft : int= 110;
private var barTop : int= 1;
private var myStyle : GUIStyle= new GUIStyle();


//assign the camera to a variable so we can raycast from it
private var myCam : GameObject;
myCam = GameObject.Find("MainCamera"); //I removed the space from the camera's name in the Unity Inspector, so you will probably need to change this

function Awake() 
{
	myTransform = transform;
	myCamera = Camera.main;
	health = 50; //arbritrarily chosen values to show that this script works
	maxHealth = 100;
}

function OnGUI()
{
	worldPosition = new Vector3(myTransform.position.x, myTransform.position.y + adjustment,myTransform.position.z);
	screenPosition = myCamera.WorldToScreenPoint(worldPosition);
	
	//creating a ray that will travel forward from the camera's position	
	var ray = new Ray (myCam.transform.position, transform.forward);
	var hit : RaycastHit;
	var forward = transform.TransformDirection(Vector3.forward);
	var distance = Vector3.Distance(myCam.transform.position, transform.position); //gets the distance between the camera, and the intended target we want to raycast to
	
	//if something obstructs our raycast, that is if our characters are no longer 'visible,' dont draw their health on the screen.
	if (!Physics.Raycast(ray, hit, distance))
	{
		GUI.color = Color.red;
		GUI.HorizontalScrollbar(Rect (screenPosition.x - healthBarLeft / 2, Screen.height - screenPosition.y - barTop, 100, 0), 0, health, 0, maxHealth); //displays a healthbar
		
		GUI.color = Color.white;
		GUI.contentColor = Color.white;					
		GUI.Label(Rect(screenPosition.x - healthBarLeft / 2, Screen.height - screenPosition.y - barTop+5, 100, 100), ""+health+"/"+maxHealth); //displays health in text format
	}
}
1 Like

Thanks!!!

An entire night wasted trying to figure out how to make this, and then i found this Thread.

Great Topic!:smile: