How do I fix so that if my enemy just touched me and stays out I'll regenerate instead of dying. And I'm trying to fix my healthgui. Help please?
[Note: It's an edited script from speed tutor's fading health]
#pragma strict
var tex1 : Texture;
var tex2 : Texture;
var tex3 : Texture;
var tex4 : Texture;
var tex5 : Texture;
var style : GUIStyle;
var health : float = 100.0;
private var dead : boolean = false;
private var maxHealth : float = 100.0;
private var enterCollider : boolean = false;
var damagePerSecondOnContact : float = 20;
var healthRegeneratedPerSecond : float = 5;
//Damage Collider
function OnTriggerEnter(Col : Collider) {
if (Col.gameObject.tag == "Enemy") {
enterCollider = true;
Debug.Log("Is inside collider");
}
}
function OnTriggerExit(Col : Collider) {
if (Col.gameObject.tag == "Enemy") {
enterCollider = false;
}
}
function Update() {
if(dead){
health = 0;
return;
}
//check for health exceeding maxHealth and correct if needed
if (health > maxHealth) {
health = maxHealth;
}
if (health > 0) {
//We are alive! Check if we should heal or take damage
if(enterCollider == true){
//take damage
health -= Time.deltaTime * damagePerSecondOnContact;
Debug.Log("Was damaged to " + health);
}
else{
//heal (a heal delay might be good, we may be regenerating faster than collisions can cause damage)
health += Time.deltaTime * healthRegeneratedPerSecond;
Debug.Log("Was healed to " + health);
}
}
else if (health <= 0) {
StartCoroutine(OnHealthZero()); //THIS WAS WHAT WAS MISSING
}
}
function OnGUI() {
if (health <= 70) {
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), tex1);
}
else if (health <= 45) {
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), tex2);
}
else if (health <= 25) {
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), tex3);
}
else if (health <= 10) {
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), tex4);
}
else if (health <= 0 || dead) {
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), tex5);
}
GUI.Box (Rect (Screen.width - 200,Screen.height-35,200,80), health.ToString(), style);
}
function OnHealthZero(){
//we indicate that the player died IMMEDIATELY so as to stop bad updates
Debug.Log("Is dead now");
dead = true;
yield WaitForSeconds(0.01);
Application.LoadLevel("Deade");
}
↧