using UnityEngine; public class HealthSystem : MonoBehaviour { [Header("Health Settings")] public int MaxHp = 20; public int CurrentHp = 20; public bool invulnerable; #if UNITY_EDITOR [HelpAttribute("Change these settings if you want to change the player portrait or player name in the healthbar located in the upperleft corner of the screen.", UnityEditor.MessageType.Info)] #endif [Header("Healthbar Settings")] public Sprite HUDPortrait; public string PlayerName; public delegate void OnHealthChange(float percentage, GameObject GO); public static event OnHealthChange onHealthChange; void Start(){ SendHealthUpdateEvent(); } //substract health public void SubstractHealth(int damage){ if(!invulnerable){ //reduce hp CurrentHp = Mathf.Clamp(CurrentHp -= damage, 0, MaxHp); //Health reaches 0 if (isDead()) gameObject.SendMessage("Death", SendMessageOptions.DontRequireReceiver); } SendHealthUpdateEvent(); } //add health public void AddHealth(int amount){ CurrentHp = Mathf.Clamp(CurrentHp += amount, 0, MaxHp); SendHealthUpdateEvent(); } //health update event void SendHealthUpdateEvent(){ float CurrentHealthPercentage = 1f/MaxHp * CurrentHp; if(onHealthChange != null) onHealthChange(CurrentHealthPercentage, gameObject); } //death bool isDead(){ return CurrentHp == 0; } }