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