Health bar goes down immediately
I had numeric health and now i want to change it to a sliding health bar using an image because a lot of people are complaining that numeric health is to distracting. I am trying to test a healthbar while still having the numeric code (for now). Instead of the healthcare going down slowly as the character gets hit it goes to 0 immediately. Please help.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public Text Health;
public GameMan gameMan;
public int maxHp = 100;
public int currentHp;
public Image currentHealthbar;
// public GameObject HealthPickup;
// Use this for initialization
void Start () {
currentHp = maxHp;
Health.text = "Health: " + maxHp;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision col)
{
Follow foll = col.gameObject.GetComponent<Follow> ();
if (foll != null)
{
Damage(foll.damage);
}
}
void OnTriggerEnter(Collider gameObject)
{
if (gameObject.tag == "health")
{
currentHp = currentHp + 1000;
}
}
void Damage(int damageToTake)
{
float ratio = 1 / maxHp;
currentHp -= damageToTake;
Health.text = "Health: " + currentHp;
currentHealthbar.rectTransform.localScale = new Vector3 (ratio, 1, 1);
if (currentHp <= 0) {
Die ();
}
}
void Die()
{
gameMan.Death ();
}
// void OnColliderEnter(Collider HealthPickup){
// Debug.Log ("health boosted");
// currentHp= 100;
// }
}
Comment
Answer by TrEVoR2004 · Jul 13, 2017 at 05:40 PM
TRY THIS!! IT'S PROBABLY NOT WHAT YOU WANT BUT IT HELPS A LOT!!