Anonim_999

Health

Feb 13th, 2022 (edited)
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3.  
  4. public class Hero : MonoBehaviour
  5. {
  6.     [SerializeField] private UnityEvent _healthChanged;
  7.     [SerializeField] private float _value;
  8.     [SerializeField] private float _maxHealth;
  9.     [SerializeField] private float _health;
  10.  
  11.     public event UnityAction HealthChanged
  12.     {
  13.         add => _healthChanged.AddListener(value);
  14.         remove => _healthChanged.RemoveListener(value);
  15.     }
  16.  
  17.     public float Health => _health;
  18.     public float MaxHealth => _maxHealth;
  19.  
  20.     public void Heal()
  21.     {
  22.         _health = Mathf.Clamp(_health + _value,0,_maxHealth);
  23.         _healthChanged?.Invoke();
  24.     }
  25.  
  26.     public void Damage()
  27.     {
  28.         _health = Mathf.Clamp(_health - _value,0,_maxHealth);
  29.         _healthChanged?.Invoke();
  30.     }
  31. }
Add Comment
Please, Sign In to add comment