Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlayerHealth : Monobehaviour
- {
- [SerializeField]
- Image[] healthContainers;
- int currentHealth;
- int maxHealth;
- int healthPerHealthContainer = 4;
- private void Start()
- {
- maxHealth = healthContainers.Length * healthPerHealthContainer;
- currentHealth = maxHealth;
- UpdateHealthBar();
- }
- public void ChangeHealth(int amount)
- {
- currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
- UpdateHealthBar();
- }
- void UpdateHealthBar()
- {
- for (int healthContainerIndex = 0; healthContainerIndex < HealthContainers.Length; healthContainerIndex++)
- {
- healthContainers[healthContainerIndex].fillAmount = Mathf.Clamp01((float)currentHealth / healthPerHealthContainer - healthContainerIndex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement