Question by
AlexTheHollow · Oct 17, 2015 at 10:16 AM ·
2dcollisiongameobjectcanvashealth
Health Bar Help (2d, Unity 5)
I have a script for a health bar but I need some help with the follower:
Display a canvas (game over menu) when the bar reaches 0
Refills health by 80 points when the player collides with a game object.
P.S. Also, if anyone could provide help with code to kill the play on contact of a certain object (detected by tag) and brings up a menu (not entirely necessary), that would be fantastic. All the tutorials online come with problems (probably because their all Unity 4.6 and below).
Code:
using UnityEngine;
using System.Collections;
public class HydrationBar : MonoBehaviour {
public float max_Health = 100f;
public float cur_Health = 0f;
public GameObject healthBar;
// Use this for initialization
void Start () {
cur_Health = max_Health;
InvokeRepeating("decreaseHealth", 1f, 1f);
}
// Update is called once per frame
void Update () {
}
void decreaseHealth()
{
cur_Health -= 5f;
float calc_Health = cur_Health / max_Health;
setHealthBar(calc_Health);
}
public void setHealthBar (float myhealth)
{
healthBar.transform.localScale = new Vector3(myhealth, healthBar.transform.localScale.y, healthBar.transform.localScale.z);
}
}
Comment