Advertisement
johnnygoodguy2000

HealthController.cs

Jun 15th, 2024
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. using UnityEngine.Events;
  6.  
  7. public class HealthController : MonoBehaviour
  8. {
  9.     [SerializeField] private float currentHealth;
  10.     [SerializeField] private float maximumHealth;
  11.  
  12.     public float RemainingHealthPercentage
  13.     {
  14.         get
  15.         {
  16.             return currentHealth / maximumHealth;
  17.         }
  18.     }
  19.  
  20.  
  21.     public bool IsInvincible { get; set; }
  22.     public UnityEvent OnDied;
  23.     public UnityEvent OnDamaged;
  24.  
  25.     public UnityEvent OnHealthChanged;
  26.  
  27.     public void TakeDamage(float damageAmount)
  28.     {
  29.         if (currentHealth == 0)
  30.         {
  31.             return;
  32.         }
  33.  
  34.         if (IsInvincible)
  35.         {
  36.             return;
  37.         }
  38.         currentHealth -= damageAmount;
  39.  
  40.         OnHealthChanged.Invoke();
  41.  
  42.  
  43.         if (currentHealth < 0)
  44.         {
  45.             currentHealth = 0;
  46.         }
  47.  
  48.         if (currentHealth == 0)
  49.         {
  50.             OnDied.Invoke();
  51.         }
  52.         else
  53.         {
  54.             OnDamaged.Invoke();
  55.         }
  56.  
  57.  
  58.     }
  59.  
  60.     public void AddHealth(float amountToAdd)
  61.     {
  62.         if (currentHealth == maximumHealth)
  63.         {
  64.             return;
  65.         }
  66.         currentHealth += amountToAdd;
  67.  
  68.         OnHealthChanged.Invoke();
  69.  
  70.         if (currentHealth > maximumHealth)
  71.         {
  72.             currentHealth = maximumHealth;
  73.         }
  74.     }
  75.  
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement