Last active
February 3, 2016 03:07
-
-
Save moon-goon/2d09c1d3b0d3ae2d63e6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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