Need help with animating HP bar
I'm trying to use a spritesheet for displaying my player's health bar. I am trying to change the animation's time when the HP variable is at a certain point so the HP sprite reflects the current HP. For some reason, the inspector is not letting me select the animation. When I go into the inspector, I can see the option to select an animation for the variable, and I can highlight it and select it, but this selection is not reflected in the editor. Yes, I have relaunched.
Script in question:
using UnityEngine;
using System.Collections;
public class emptyHealth : MonoBehaviour {
public int hP = 3;
public Animation anim;
void LOSEONE() {
hP -= 1;
}
void Update () {
if (hP == 3) {
anim["health"].time = 0;
}
if (hP == 2) {
anim["health"].time = 0.5f;
}
if (hP == 1) {
anim["health"].time = 0.75f;
}
if (hP == 0 || hP <= 0) {
anim["health"].time = 1f;
}
}
}
Answer by woo-hee-kwon · Oct 18, 2016 at 05:56 AM
public float curHp;
public float oldHp;
public float percentHp;
public GameObject GageObject;
void Start(){
curHp = 3;
oldHp = curHp;
}
void Update(){
percentHp = curHp / oldHp;
GageObject.transform.localScale = new Vector3( percentHp, 1, 1) ;
}