using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine; public class DoorHealthBarManager : MonoBehaviour { public Color FullHealthColor; public Color EmptyHealthColor; public GameObject Bar; public GameObject DoorIcon; public Sprite DoorSprite100; public Sprite DoorSprite50; public Sprite DoorSprite25; private Image barImage; private SpriteRenderer spriteRenderer; private Image doorImage; private int healthValue; private Transform playerPos; // Start is called before the first frame update private void Start() { barImage = Bar.GetComponent(); doorImage = DoorIcon.GetComponent(); healthValue = 100; UpdateHealthValue(healthValue); playerPos = GameObject.FindGameObjectWithTag("Player").transform; // UpdateHealthValue(10); // For testing purpose only } private void Update() { transform.LookAt(playerPos); } public void UpdateHealthValue(int health) { if(health >= 0 && health <= 100) // Health value must be an integer between 0 and 100 { healthValue = health; float healthAmount = (float)healthValue / 100f; barImage.fillAmount = healthAmount; Color lerpedColor = Color.Lerp(FullHealthColor, EmptyHealthColor, 1.0f - healthAmount); barImage.color = lerpedColor; Debug.Log("healthAmount = " + healthAmount); Debug.Log(lerpedColor.r + ", " + lerpedColor.g + ", " + lerpedColor.b); if (healthValue > 50.0f) { SetIconSprite(DoorSprite100); } else if (healthValue > 25.0f) { SetIconSprite(DoorSprite50); } else { SetIconSprite(DoorSprite25); } } } private void SetIconSprite(Sprite doorSprite) { doorImage.sprite = doorSprite; } }