Help with a health bar
Hi there. I am working on a 2.5D platformer in Unity 5.5.1. I have a script for an auto-healing health bar, but I want it to work so that the player character will take damage when attacked by an enemy. So far, I only have it programmed so that damage can be taken with the press of a button. Here is my script: (it's in Java)
var ls:UnityEngine.SceneManagement.SceneManager;
var health = 300; var damaged = false;
function Update (){ if(Input.GetButtonDown("C")){
playerDamage();
}
if(damaged == false){
if(health < 300){
health += 1;
}
if(health <= 1){
ls.LoadScene("MissionFailed");
}
}
}
function playerDamage(){ damaged = true; health -= 30; yield WaitForSeconds(1); damaged = false; }
function OnGUI(){ if (damaged == true) { GUI.backgroundColor = Color.red; } else { GUI.backgroundColor = Color.green; } GUI.Button(Rect(20,20,health,20), "health"); }
Any help is always appreciated, so thank you :)