# Health bar over enemy ?

**URL:** https://discussions.unity.com/t/health-bar-over-enemy/394423
**Category:** Scripting
**Tags:** Scripting
**Created:** [July 5, 2009, 9:03pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423 "2009-07-05T21:03:46Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_92685416](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anon\_92685416](https://discussions.unity.com/u/anon_92685416)
#### Post date: [July 5, 2009, 9:03pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/1 "2009-07-05T21:03:46Z")

</div>

Hi, I’m making a 3rd person game and want to know how to make the life of my enemies showing over there heads. Like a floating health bar over all the enemies

---

<div class="post-metadata">

### Author: ![User340](https://avatars.discourse-cdn.com/v4/letter/u/bbe5ce/32.png) [@User340](https://discussions.unity.com/u/User340)
#### Post date: [July 6, 2009, 3:35am UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/2 "2009-07-06T03:35:47Z")

</div>

First create a guiTexture. Then to make it track a gameobject, put the following script onto it.

```auto
// This script will make a GUITexture follow a transform.
var target : Transform;

function Update ()
{
     var wantedPos = Camera.main.WorldToScreenPoint (target.position);
     transform.position = wantedPos;
}

```

---

<div class="post-metadata">

### Author: ![anon\_92685416](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anon\_92685416](https://discussions.unity.com/u/anon_92685416)
#### Post date: [July 6, 2009, 10:31am UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/3 "2009-07-06T10:31:07Z")

</div>

Thanks, I’ve tested it but it isn’t visible when I start the game.

It’s just getting away …

Do I need any other codes ?

---

<div class="post-metadata">

### Author: ![Martin-Schultz](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/martin-schultz/32/572373_2.png) [@Martin-Schultz](https://discussions.unity.com/u/Martin-Schultz)
#### Post date: [July 6, 2009, 11:29am UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/4 "2009-07-06T11:29:53Z")

</div>

No, Daniels code is fine. I guess you have not set the “target” variable in the inspector to the object where the health bar should appear.

---

<div class="post-metadata">

### Author: ![anon\_92685416](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anon\_92685416](https://discussions.unity.com/u/anon_92685416)
#### Post date: [July 6, 2009, 12:34pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/5 "2009-07-06T12:34:09Z")

</div>

The things that I have done is to,  
make a GUItexture then make a new script, put in his code, put the script on the texture, drag the enemy into the target variable, start the game and nothing happend …

---

<div class="post-metadata">

### Author: ![Maker16](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@Maker16](https://discussions.unity.com/u/Maker16)
#### Post date: [July 6, 2009, 2:07pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/6 "2009-07-06T14:07:14Z")

</div>

I had a similar problem trying to get the FPS script to work (it uses a GUITexture). I never was able to get it to display.

---

<div class="post-metadata">

### Author: ![User340](https://avatars.discourse-cdn.com/v4/letter/u/bbe5ce/32.png) [@User340](https://discussions.unity.com/u/User340)
#### Post date: [July 6, 2009, 4:12pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/7 "2009-07-06T16:12:16Z")

</div>

> [@""](#):
>
> The things that I have done is to,  
> make a GUItexture then make a new script, put in his code, put the script on the texture, drag the enemy into the target variable, start the game and nothing happend …

Oops, I gave you bad code. Try this one instead:

```auto
var target : Transform; 

function Update () 
{ 
     var wantedPos = Camera.main.WorldToViewportPoint (target.position); 
     transform.position = wantedPos; 
}

```

---

<div class="post-metadata">

### Author: ![anon\_92685416](https://avatars.discourse-cdn.com/v4/letter/a/b782af/32.png) [@anon\_92685416](https://discussions.unity.com/u/anon_92685416)
#### Post date: [July 6, 2009, 5:28pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/8 "2009-07-06T17:28:44Z")

</div>

Hey, now it’s working …  
is it a easy way to make the text/texture not be visible over the player.  
Now it,s like a regular GUI on top of everything …

THANKS …

---

<div class="post-metadata">

### Author: ![Discord](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/discord/32/568561_2.png) [@Discord](https://discussions.unity.com/u/Discord)
#### Post date: [July 6, 2009, 8:10pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/9 "2009-07-06T20:10:29Z")

</div>

You could always make a 2d plane and have it attached to the enemy. Then have it rotate to always face the player. Then when you damage the enemy, scale down the plane to simulate it shrinking.

---

<div class="post-metadata">

### Author: ![skoandi\_1](https://avatars.discourse-cdn.com/v4/letter/s/bbce88/32.png) [@skoandi\_1](https://discussions.unity.com/u/skoandi_1)
#### Post date: [October 20, 2012, 2:03pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/10 "2012-10-20T14:03:07Z")

</div>

Thanks for the script! 🙂

---

<div class="post-metadata">

### Author: ![Voidexx](https://avatars.discourse-cdn.com/v4/letter/v/258eb7/32.png) [@Voidexx](https://discussions.unity.com/u/Voidexx)
#### Post date: [January 21, 2014, 6:00am UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/11 "2014-01-21T06:00:56Z")

</div>

I have a Quick Question!  
haha, I made my own health bar for my character or the player that you’re playing Which is right here

```auto
using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour {
	public int maxHealth = 100;
	public int curHealth = 100;
	
	public float healthBarLength;
	
	// Use this for initialization
	void Start () {
		healthBarLength = Screen.width / 6;
	}
	
	// Update is called once per frame
	void Update () {
		AddjustCurrentHealth(0);	
	}
	
	void OnGUI() {
		GUI.Box(new Rect(700, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
	}
	
	public void AddjustCurrentHealth(int adj) {
		curHealth += adj;
		
		if (curHealth < 0)
			curHealth = 0;
		
		if (curHealth > maxHealth)
			curHealth = maxHealth;
		
		if(maxHealth < 1)
			maxHealth = 1;
		
		healthBarLength = (Screen.width / 6) * (curHealth /(float)maxHealth);
	}
}

```

Now how would I implement your code into this to show the health above the enemy?

THANKS IN ADVANCE

---

<div class="post-metadata">

### Author: ![Zakalakin](https://avatars.discourse-cdn.com/v4/letter/z/eb8c5e/32.png) [@Zakalakin](https://discussions.unity.com/u/Zakalakin)
#### Post date: [February 11, 2014, 6:22pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/12 "2014-02-11T18:22:07Z")

</div>

> [@Voidexx](#):
>
> I have a Quick Question!  
> haha, I made my own health bar for my character or the player that you’re playing Which is right here
> 
> ```auto
> using UnityEngine;
> using System.Collections;
> 
> public class EnemyHealth : MonoBehaviour {
> public int maxHealth = 100;
> public int curHealth = 100;
> 	
> public float healthBarLength;
> 	
> // Use this for initialization
> void Start () {
> healthBarLength = Screen.width / 6;
> }
> 	
> // Update is called once per frame
> void Update () {
> AddjustCurrentHealth(0);	
> }
> 	
> void OnGUI() {
> GUI.Box(new Rect(700, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
> }
> 	
> public void AddjustCurrentHealth(int adj) {
> curHealth += adj;
> 		
> if (curHealth < 0)
> curHealth = 0;
> 		
> if (curHealth > maxHealth)
> curHealth = maxHealth;
> 		
> if(maxHealth < 1)
> maxHealth = 1;
> 		
> healthBarLength = (Screen.width / 6) * (curHealth /(float)maxHealth);
> }
> }
> 
> ```
> 
> Now how would I implement your code into this to show the health above the enemy?
> 
> THANKS IN ADVANCE

Replace your GUI with this, and It’ll work; c#

```auto
	void OnGUI()
	{
	
        Vector2 targetPos;
	targetPos = Camera.main.WorldToScreenPoint (transform.position);
		
	GUI.Box(new Rect(targetPos.x, Screen.height- asd.y, 60, 20), health + "/" + maxHealth);
		
	}

```

---

<div class="post-metadata">

### Author: ![Izzy4me](https://avatars.discourse-cdn.com/v4/letter/i/e56c9b/32.png) [@Izzy4me](https://discussions.unity.com/u/Izzy4me)
#### Post date: [August 20, 2014, 1:26pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/13 "2014-08-20T13:26:38Z")

</div>

I know it is old topic but i have a question about _Daniel_’s “good” simple script - how to display only one health bar? (I see another health bar if I turn around). It is possible or I should use Rect object or creating prefabs of my bar? I really enjoyed 2D bars (expecially for testing).

---

<div class="post-metadata">

### Author: ![Cha0os](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@Cha0os](https://discussions.unity.com/u/Cha0os)
#### Post date: [August 20, 2014, 1:45pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/14 "2014-08-20T13:45:43Z")

</div>

_Daniel_’s script only moves a GUITexture so that it is displayed “on top” of a Target-Transform.  
So it will only display one Health Bar if that’s what your GUITexture is.

If you want to show multiple Health Bars you will have to add more GUITextures and attach the script to them.  
If you want to move the Health Bar to a different Object when certain conditions are met, you will need to update the “target”-variable accordingly.

---

<div class="post-metadata">

### Author: ![Izzy4me](https://avatars.discourse-cdn.com/v4/letter/i/e56c9b/32.png) [@Izzy4me](https://discussions.unity.com/u/Izzy4me)
#### Post date: [August 20, 2014, 2:41pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/15 "2014-08-20T14:41:41Z")

</div>

It’s not exacly that I wanted to know.

1. I created Cube1 with texture and movingScript
2. I create new object of GUITexture and add my .png file to it (at field “Texture”)
3. GUITexture was moved in hierarchy panel to be child of Cube1 object.
4. I created new JS script and paste that:

```auto
var target : Transform;

function Update (){
         var wantedPos = Camera.main.WorldToViewportPoint (target.position);
         transform.position = wantedPos;
}

```

1. I dropped script on the GUITexture object.
2. As “Target” I choose Cube1 object.

When I’m playing I have my bar in right place AND behind me. It’s because WorldToScreenPoint ? It has problems with Y asis and duplicate texture (it’s 3D game problem)?

---

<div class="post-metadata">

### Author: ![joandku](https://avatars.discourse-cdn.com/v4/letter/j/f07891/32.png) [@joandku](https://discussions.unity.com/u/joandku)
#### Post date: [November 9, 2014, 11:19pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/16 "2014-11-09T23:19:10Z")

</div>

what is the target supposed to do it made my enemy disapear  
🕶

---

<div class="post-metadata">

### Author: ![nicoguerra1337](https://avatars.discourse-cdn.com/v4/letter/n/3ab097/32.png) [@nicoguerra1337](https://discussions.unity.com/u/nicoguerra1337)
#### Post date: [November 20, 2014, 11:56pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/17 "2014-11-20T23:56:11Z")

</div>

I found a fix for the GUI appearing from the side of the screen. Add this code in the Update() function of your script:

(CSharp and JavaScript have the same code)

```csharp
if (renderer.isVisible)
  drawGui = true;
else
  drawGui = false;

```

---

<div class="post-metadata">

### Author: ![KeinZantezuken](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@KeinZantezuken](https://discussions.unity.com/u/KeinZantezuken)
#### Post date: [July 12, 2017, 6:05pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/18 "2017-07-12T18:05:37Z")

</div>

Any ideas how to calculate position the GUI part to be “over the head” of the sprite (using Camera.main.WorldToScreenPoint() here as well)? The GO coordinates are somewhere in the middle of it, so I need to offset them but not sure how yet. Should I calculate the height of a sprite in pixels and then deduct it from y?

---

<div class="post-metadata">

### Author: ![unity\_yclhduH3WcJO0A](https://avatars.discourse-cdn.com/v4/letter/u/5e9695/32.png) [@unity\_yclhduH3WcJO0A](https://discussions.unity.com/u/unity_yclhduH3WcJO0A)
#### Post date: [March 12, 2019, 4:49pm UTC](https://discussions.unity.com/t/health-bar-over-enemy/394423/19 "2019-03-12T16:49:16Z")

</div>

> [@Izzy4me](#):
>
> It is

lol k
