Skip to content

Instantly share code, notes, and snippets.

@fiskefyren
Created November 13, 2015 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiskefyren/1f405ddc833bf78fa446 to your computer and use it in GitHub Desktop.
Save fiskefyren/1f405ddc833bf78fa446 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public float hitPoints = 100f;//max health
public float currentHitPoints;//current health
public GameObject destroyFX;//not sure why you use GameObject rather than Transform...
public GameObject healthbar;
public Color32 startColor;
public Color32 middleColor;
public Color32 endColor;
void Start () {
currentHitPoints = hitPoints;//starting health, which is max health
}
void TakeDamage(float amt) { //amt=amount of health
currentHitPoints -= amt;
//float healthCalc = currentHitPoints / hitPoints; //if currentHitPoints 80 / 100 = 0.8f //this will decrease the healthbar when taking damage
//setHealthbar(healthCalc);
setHealthbar();
if (currentHitPoints <=0) {
currentHitPoints = 0;
Die();
}
}
void Die() {
if(gameObject.tag == "Enemy") {
//Instantiate(destroyFX, this.transform.position, this.transform.rotation); <- need an explosion! not sure why we need keyword: "this."
Destroy (gameObject);
//Debug.Log ("Enemy Object Has died");
}
}
public void setHealthbar (float enemyHealth) {
//enemyHealth valve 0-1, <- health goes from 0=0% to 1=100%?
float healthCalc = currentHitPoints / hitPoints;
healthbar.transform.localScale = new Vector3(enemyHealth, healthbar.transform.localScale.y, healthbar.transform.localScale.z);
if (healthCalc < 0.5f) {
healthbar.color = Color.Lerp(endColor, middleColor, healthCalc * 2);
}
else {
healthbar.color = Color.Lerp(middleColor, startColor, (healthCalc - 0.5f) * 2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment