I feel kinda dumb, but I have no idea how this health bar is working in terms of the math. Between these two scripts. Anyone able to help explain?
- PlayerHealthBar script
[RequireComponent(typeof(RawImage))]
public class PlayerHealthBar : MonoBehaviour
{
RawImage healthBarRawImage;
Player player;
// Use this for initialization
void Start()
{
player = FindObjectOfType<Player>();
healthBarRawImage = GetComponent<RawImage>();
}
// Update is called once per frame
void Update()
{
float xValue = -(player.healthAsPercentage / 2f) - 0.5f;
healthBarRawImage.uvRect = new Rect(xValue, 0f, 0.5f, 1f);
}
}
- Player Script
public class Player : MonoBehaviour
{
[SerializeField] float maxHealthPoints = 100f;
float currentHealthPoints = 100f;
public float healthAsPercentage
{
get
{
return currentHealthPoints / (float)maxHealthPoints;
}
}
}