using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Sirenix.OdinInspector; namespace KairoEngine.CharacterSystem { public class HealthBarUi : MonoBehaviour { [BoxGroup("State"), ReadOnly] public Transform target; [BoxGroup("State"), ReadOnly] public float valueForeground = 50f; [BoxGroup("State"), ReadOnly] public float valueBackground = 150f; [FoldoutGroup("Configurations")] public RectTransform rectTransform; [FoldoutGroup("Configurations")] public RectTransform foregroundObj; [FoldoutGroup("Configurations")] public RectTransform backgroundObj; [FoldoutGroup("Configurations")] public Image foregroundImg; [FoldoutGroup("Configurations")] public Image backgroundImg; [FoldoutGroup("Configurations")] public float hideTime = 2f; private float hideTimeCounter = 0f; public void Setup(DamageController damageController) { hideTimeCounter = 0f; target = damageController.transform; valueForeground = damageController.HP; valueBackground = damageController.maxHP; } public void Update() { if (target != null && rectTransform != null) { Vector3 headPos = new Vector3(target.position.x, target.position.y + 2.2f, target.position.z); Vector3 screenPos = Camera.main.WorldToScreenPoint(headPos); rectTransform.position = screenPos; SetPercentage(); } hideTimeCounter += Time.deltaTime; if(hideTimeCounter >= hideTime) { hideTimeCounter = 0f; gameObject.SetActive(false); } } void SetPercentage() { float width = backgroundObj.rect.width; float newWidth = (width * valueForeground) / valueBackground; if (newWidth > width) newWidth = width; if (newWidth < 0) newWidth = 0; foregroundObj.sizeDelta = new Vector2(newWidth, backgroundObj.rect.height); } } }