Created
November 16, 2015 21:35
-
-
Save fiskefyren/222211dd58c86b36f0ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
public class EnemyHealth : MonoBehaviour { | |
public float hitPoints = 100f;//max health | |
public float currentHitPoints;//current health | |
public GameObject destroyFX; | |
public Image healthbar; | |
public Color32 startColor; | |
public Color32 middleColor; | |
public Color32 endColor; | |
void Start () { | |
currentHitPoints = hitPoints;//starting health, which is max health | |
setHealth(); | |
} | |
void TakeDamage(float amt) { //amount of health | |
currentHitPoints -= amt; | |
setHealth(); | |
if (currentHitPoints <=0) { | |
currentHitPoints = 0; | |
Die(); | |
} | |
} | |
void Die() { | |
if(gameObject.tag == "Enemy") { | |
//Instantiate(destroyFX, this.transform.position, this.transform.rotation); | |
Destroy (gameObject); | |
//Debug.Log ("Enemy Object Has died"); | |
} | |
} | |
public void setHealth() { | |
//this will decrease the healthbar when taking damage | |
float enemyHealthCalc = currentHitPoints / hitPoints; //if currentHitPoints 80 / 100 = 0.8f | |
//enemyHealth valve 0-1, <- health goes from 0=0% to 1=100%? | |
healthbar.transform.localScale = new Vector3(enemyHealthCalc, healthbar.transform.localScale.y, healthbar.transform.localScale.z); | |
if (enemyHealthCalc < 0.1f) { | |
healthbar.color = Color.Lerp(endColor, middleColor, enemyHealthCalc * 2); | |
//Debug.Log("color code 'if' is running"); | |
} | |
else { | |
healthbar.color = Color.Lerp(middleColor, startColor, (enemyHealthCalc - 0.5f) * 2); | |
//Debug.Log("color code 'else' is running"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment