Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. public class PlayerHealth : Monobehaviour
  2. {
  3.     [SerializeField]
  4.     Image[] healthContainers;
  5.  
  6.     int currentHealth;
  7.     int maxHealth;
  8.     int healthPerHealthContainer = 4;
  9.  
  10.     private void Start()
  11.     {
  12.         maxHealth = healthContainers.Length * healthPerHealthContainer;
  13.         currentHealth = maxHealth;
  14.         UpdateHealthBar();
  15.     }
  16.  
  17.     public void ChangeHealth(int amount)
  18.     {
  19.         currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
  20.         UpdateHealthBar();
  21.     }
  22.  
  23.     void UpdateHealthBar()
  24.     {
  25.         for (int healthContainerIndex = 0; healthContainerIndex < HealthContainers.Length; healthContainerIndex++)
  26.         {
  27.             healthContainers[healthContainerIndex].fillAmount = Mathf.Clamp01((float)currentHealth / healthPerHealthContainer - healthContainerIndex);
  28.         }
  29.     }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement