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;
}
↧