Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- [RequireComponent(typeof(Slider))]
- public class HealthBar : MonoBehaviour
- {
- [SerializeField] private Hero _player;
- [SerializeField] private float _maxDelta;
- private Slider _slider;
- private void OnEnable()
- {
- _player.HealthChanged += StartChangeValue;
- _slider = GetComponent<Slider>();
- _slider.maxValue = _player.MaxHealth;
- _slider.value = _player.Health;
- }
- private void OnDisable()
- {
- _player.HealthChanged -= StartChangeValue;
- }
- public void StartChangeValue()
- {
- StartCoroutine(ChangeValue());
- }
- private IEnumerator ChangeValue()
- {
- while (_slider.value != _player.Health)
- {
- _slider.value = Mathf.MoveTowards(_slider.value, _player.Health,_maxDelta);
- yield return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment