- Home /
Healthy Floating Numbers
I have this code attached to some enemy characters in my scene. I was wondering if there was a way to have a health bar or health number just floating above the enemy characters?
var hitPoints = 100.0;
var deadReplacement : Transform;
var dieSound : AudioClip;
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0)
{
Detonate();
}
}
function Detonate () {
// Destroy ourselves
Destroy(gameObject);
// Play a dying audio clip
if (dieSound)
AudioSource.PlayClipAtPoint(dieSound, transform.position);
// Replace ourselves with the dead body
if (deadReplacement) {
var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) {
dst.position = src.position;
dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
Answer by MithosAnnar · Feb 24, 2012 at 07:50 PM
Attach this script to your enemy.
EnemyHealth.js:
/* Attach to the enemy */
//the amount of health the enemy has
var hP : float = 100;
//the amount of health the enemy should not have more of
var maxHP : float = 100;
//the width your bar is
var healthBarWidth : int;
//the texture you wish to have on
var EnemyHealthTexture : Texture;
//allows the bar to be seen when set to true
var HealthEnabled : boolean;
function Start () {
//whatever length you want your bar to start at
healthBarWidth = 131;
//by default we can't see the health bar
HealthEnabled = false;
}
function Update () {
//the percent will update the length of the bar
var healthpercent : float = hP / maxHP;
if (healthpercent < 0) { healthpercent = 0; }
if (healthpercent > 100) { healthpercent = 100; }
//makes sure the bar is the correct ratio
healthBarWidth = healthpercent * 131;
}
function OnGUI () {
//if Activated show the health Bar
if (HealthEnabled == true) {
//Draw the health bar, set it to whatever position and size
GUI.DrawTexture( Rect(60, 50, healthBarWidth, 17), EnemyHealthTexture);
}
}
//when you target the enemy you also SendMessage("Activate");
function Activate () {
//allows the GUI to be Shown
HealthEnabled = true;
}
Answer by Berenger · Feb 21, 2012 at 06:18 AM
First I thought you were asking for variables of type float good for your health ^^
Anyway, Health bar is done can be displaying a white texture with a tint on Gui, rect.width being that health. Displaying health over a character's head, look Camera.ScreenPointToRay.
haha!
I am pretty new at scripting.... How would I do that?
Health bar with GUI :
Unity let you implement a function called OnGUI which will be called by Unity to display all the gui stuff. One way to display a texture is, inside OnGUI(), to call GUI.DrawTexture( rect, tex ); By modifying the width value of rect, you will then change the size of the health bar.
Another aspect of the GUI is GUI.color. That color will be multiplied to every GUI elements. It's set to white by unity before every OnGUI calls, so it doesn't change anything unless you want to. So if you display a white texture and set GUI.color to blue, your health bar is displayed blue.
Display something over the character's head
The challenge here is to find that head screen position. For that, you can use Camera.WorldToScreenPoint, which for a Vector3 in world space give you a Vector2 in screen space (0,0)->(resW, resH). Once you have that information you can display the text. Now a GUI is displayed from it's top left corner, so you'll need to center it. Last detail, don't call WorldToScreenPoint from OnGUI because the function is called twice by frame for GUI layout reasons, call it from update ins$$anonymous$$d.
Answer by sketchers1 · Feb 25, 2012 at 05:41 PM
Thanks! I combined Your Script with mine to get something that works. I still have one problem though. I have multiple enemies. If I partially kill one then start killing another one, the health seems to jump up to 100% again. Is there a way to have separate health bars appear for different enemies? I have attached the script that I used:
var hitPoints : float = 100.0; var deadReplacement : Transform; var dieSound : AudioClip; var healthBarWidth : int; var EnemyHealthTexture : Texture; var HealthEnabled : boolean; var maxHP : float = 100;
function Start () {
//whatever length you want your bar to start at healthBarWidth = 131;
//by default we can't see the health bar HealthEnabled = false;
}
function Update () {
//the percent will update the length of the bar var healthpercent : float = hitPoints / maxHP;
if (healthpercent < 0) { healthpercent = 0; } if (healthpercent > 100) { healthpercent = 100; }
//makes sure the bar is the correct ratio healthBarWidth = healthpercent * 131; }
function OnGUI () {
//if Activated show the health Bar if (HealthEnabled == true) {
//Draw the health bar, set it to whatever position and size
GUI.DrawTexture( Rect(60, 50, healthBarWidth, 17), EnemyHealthTexture);
}
}
function ApplyDamage (damage : float) { // We already have less than 0 hitpoints, maybe we got killed already? if (hitPoints <= 0.0) return;
hitPoints -= damage;
if (hitPoints <= 0.0)
{
Detonate();
}
//allows the GUI to be Shown HealthEnabled = true;
}
function Detonate () { // Destroy ourselves Destroy(gameObject);
// Play a dying audio clip
if (dieSound)
AudioSource.PlayClipAtPoint(dieSound, transform.position);
// Replace ourselves with the dead body
if (deadReplacement) {
var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) { dst.position = src.position; dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
Ok, do this:
1 - attach the script to each enemy.
2 - after the lines:
if (healthpercent < 0) { healthpercent = 0; } if (healthpercent > 100) { healthpercent = 100; }
write: Debug.Log(healthpercent);
3 - tell me what pops up after that.