- Home /
Question by
Robwhitlow3 · Jan 30, 2012 at 10:00 PM ·
destroyenemyhealth
Enemy health bar over them and destroy actor
Hi I am having trouble adding the health bar so it appears OVER the enemy, the script I have puts it in the top of the screen. I want it so when I am so far away its either not visible or it is very small above their head. And also when their health reaches 0 the enemy is destroyed. And I have multiple enemies so how can I make it to where all aren't destroyed? Here is my script, and thanks for your help! I know its a lot!
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 / 2;
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI() {
GUI.Box(new Rect(10, 40, 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 / 4) * (curHealth / (float)maxHealth);
}
}
Comment
Your answer
Follow this Question
Related Questions
health code help 1 Answer
Enemy health and bullet problem 2 Answers
Enemy Health Problems 1 Answer
Shoot. Just Shoot 5 Answers