Advertisement
holllowknight

Unity GUI 2

Jan 5th, 2022
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5.  
  6. public class HealthBar : MonoBehaviour
  7. {
  8.     [SerializeField] private int _maxHealh;
  9.     private int _currentHealth;
  10.  
  11.     void Start()
  12.     {
  13.         _currentHealth = _maxHealh;
  14.     }
  15.  
  16.     public void Increase(int points)
  17.     {
  18.         _currentHealth += points;
  19.         if (_currentHealth > _maxHealh)
  20.             _currentHealth = _maxHealh;
  21.         StartAnimation();
  22.     }
  23.  
  24.     public void Decrease(int points)
  25.     {
  26.         _currentHealth -= points;
  27.         if (_currentHealth < 0)
  28.             _currentHealth = 0;
  29.         StartAnimation();
  30.     }
  31.  
  32.     private void StartAnimation()
  33.     {
  34.         float newScale = (float)_currentHealth / (float)_maxHealh;
  35.         transform.DOScaleX(newScale, 1);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement