I have used UI heart health système (Unity 5 Tutorial: Heart health - like in Zelda or Minecraft C# - YouTube) :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HeartSystem : MonoBehaviour {
private int maxHeartAmount = 6;
public int startHearts = 3;
public int curHealth;
private int maxHealth;
private int healthPerHeart = 1;
int damage = 0.5;
public Image[] healthImages;
public Sprite[] healthSprites;
// Use this for initialization
void Start () {
curHealth = startHearts * healthPerHeart;
maxHealth = maxHeartAmount * healthPerHeart;
checkHealthAmount ();
}
void checkHealthAmount(){
for (int i = 0; i < maxHeartAmount; i++) {
if (startHearts <= i) {
healthImages *.enabled = false;*
-
}*
-
else {*
_ healthImages .enabled = true;_
* }*
* }*
* UpdateHearts ();*
* }*
* void UpdateHearts(){*
* bool empty = false;*
* int i = 0;*
* foreach (Image image in healthImages) {*
* if (empty) {*
* image.sprite = healthSprites [0];*
* }*
* else {*
* i++;*
_ if (curHealth >= i * healthPerHeart) {_
* image.sprite = healthSprites [healthSprites.Length - 1];*
* }*
* else {*
_ int currentHeartHealth = (int)(healthPerHeart - (healthPerHeart * i - curHealth));
* int healthPerImage = healthPerHeart / (healthSprites.Length - 1);*
* int imageIndex = currentHeartHealth / healthPerImage ;*
* image.sprite = healthSprites [imageIndex];*
* empty = true;*
* } *
* }*
* }*
* }*
* void OnColliderEnter(Collider Lost){*
* if (Lost.gameObject.tag == “LoseFoodCol”) {*
* curHealth = curHealth - 1;*
* if (curHealth <= 0) {*
* Debug.Log (“Game Over!”);*
* }*
* }*
* if (Lost.gameObject.tag == “WinFoodCol”) {*
* curHealth = curHealth + 1;*
* }*
* UpdateHearts ();*
* }*
* public void TakeDamage(int amount){*
* curHealth += amount;*
curHealth = Mathf.Clamp (curHealth, 0, startHearts * healthPerHeart);
* UpdateHearts ();*
* }*
* public void AddHeartContainer (){*
* startHearts++;*
* startHearts = Mathf.Clamp (startHearts, 0, maxHeartAmount);*
//curHealth = startHearts * healthPerHeart;
//maxHealth = maxHeartAmount * healthPerHeart;_
* checkHealthAmount ();*
* }*
}
But, I don’t know how to work this health script with a collider and collision.
So I put another script on my player
using UnityEngine;
using System.Collections;
public class HeartSystemCol : MonoBehaviour {
* public GameObject collisionObject;*
* float damage = -1f;*
* float healing = 1f;*
* // Use this for initialization*
* void Start () {*
* }*
* // Update is called once per frame*
* void Update () {*
* }*
* void OnCollisionEnter (Collision col){*
* if (col.gameObject.tag == “LoseFoodCol” ) {*
* Destroy (col.gameObject);*
* print (“vous avez perdu un coeur”);*
* }*
* if (col.gameObject.tag == “WinFoodCol”) {*
* Destroy (col.gameObject);*
* print (“vous avez gagné un coeur”);*
* }*
* if (col.gameObject.tag == “LightCol”) {*
* Destroy (col.gameObject);*
* }*
* }*
}
But, It doesn’t work.
I would like to know how to write a script to lose a half-life when Player is on collision with an object.
and also to win a life with another object.
Please help me …!
Thanks a lot