jimmy tremblay-Bernier 86fbe09b20 initial commit
2022-03-12 20:32:56 -05:00

46 lines
1.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
// Code sourced from Jason Welmann's tutorial "Health Bars in Unity3D - Quick, Clean, Easy" https://www.youtube.com/watch?v=CA2snUe7ARM&ab_channel=JasonWeimann
public class HealthBar : MonoBehaviour
{
[SerializeField]
private Image foregroundImage;
[SerializeField]
private float updateSpeedSeconds = 0.5f;
private void Awake()
{
GetComponentInParent<Enemy>().OnHealthChanged += HandleHealthChanged;
}
private void HandleHealthChanged(float pct)
{
StartCoroutine(ChangeToPct(pct));
}
private IEnumerator ChangeToPct(float pct)
{
float preChangePct = foregroundImage.fillAmount;
float elapsed = 0f;
while (elapsed < updateSpeedSeconds)
{
elapsed += Time.deltaTime;
foregroundImage.fillAmount = Mathf.Lerp(preChangePct, pct, elapsed / updateSpeedSeconds);
yield return null;
}
foregroundImage.fillAmount = pct;
}
private void LateUpdate()
{
transform.LookAt(GameObject.Find("OmniCharacterController/CameraSetupParent/CameraSetupRootOffset/[CameraRig]/Camera").transform);
transform.Rotate(0, 180, 0);
}
}