Skip to content

Instantly share code, notes, and snippets.

@moon-goon
Last active February 3, 2016 03:07
Show Gist options
  • Save moon-goon/2d09c1d3b0d3ae2d63e6 to your computer and use it in GitHub Desktop.
Save moon-goon/2d09c1d3b0d3ae2d63e6 to your computer and use it in GitHub Desktop.
public Image healthBar;
public float maximumHealth = 100f;
public float currentHealth = 0f;
void Start() {
currentHealth = maximumHealth;
}
void Update() {
if (currentHealth < 30.0f) {
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
if (currentHealth < 0.00f) {
GameObject.Destroy(gameObject);
}
}
public void onHit() {
currentHealth -= 10f; // let's say the damage will be -10 per collision
float calculate = currentHealth / maximumHealth;
onSet(calculate);
}
void onSet(float hp) {
healthBar.fillAmount = hp;
}
void OnCollisionEnter(Collision target) {
// create a gameobject (a cube for this demo) name it "enemy"
// and your argument(target) is setup so that object can be collided with
if (target.gameObject.name == "enemy") {
onHit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment