Health bar over enemy ?

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

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

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

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

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 ?

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.

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 …

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.

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

var target : Transform; 

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

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 …

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.

1 Like

Thanks for the script! :slight_smile:

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

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#

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

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).

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.

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:
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)?

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

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)

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

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?

lol k