x2Jamille

SmoothHealthBar

Oct 30th, 2020
637
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using DG.Tweening;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4.  
  5. public class HealthBar : MonoBehaviour
  6. {
  7.     [SerializeField] private float _damageAmount;
  8.     [SerializeField] private float _healAmount;
  9.     private Slider _healthBarSlider;
  10.     private float _currentHealthAmount;
  11.    
  12.  
  13.     private void Start()
  14.     {
  15.         _healthBarSlider = GetComponent<Slider>();
  16.         _healthBarSlider.value = _healthBarSlider.maxValue;
  17.         _currentHealthAmount = _healthBarSlider.maxValue;
  18.     }
  19.  
  20.     public void Heal()
  21.     {
  22.         if (_currentHealthAmount + _healAmount >= _healthBarSlider.maxValue)
  23.             _currentHealthAmount = _healthBarSlider.maxValue;
  24.         else
  25.             _currentHealthAmount += _healAmount;
  26.     }
  27.  
  28.     public void TryApplyDamage()
  29.     {
  30.         if (_currentHealthAmount - _damageAmount >= _healthBarSlider.minValue)
  31.             _currentHealthAmount -= _damageAmount;
  32.     }
  33.  
  34.     public void UpdateHealthBar()
  35.     {
  36.         if (_healthBarSlider.value != _currentHealthAmount)
  37.             _healthBarSlider.DOValue(_currentHealthAmount, 1f).SetEase(Ease.Linear);
  38.     }
  39. }
RAW Paste Data