Health
Health holds an object’s health and applies damage and healing. It derives from Modifiable, so health modifiers on the same GameObject can change incoming damage.
Fields
| Field | Type | Description |
|---|---|---|
_health | float | Starting and maximum health. |
_destroyOnDeath | bool | Destroy the GameObject when health reaches zero. |
OnHealthChanged | UnityEvent<float> | Invoked when health changes, with health from 0 to 1. |
OnDeath | UnityEvent<DamageData> | Invoked once when health reaches zero, with the killing blow (DamageData.Empty after Kill()). |
Properties
| Property | Type | Description |
|---|---|---|
maxHealth | float | Maximum health, set from the starting health on Awake. |
currentHealth | float | Current health. |
isDead | bool | True once health has reached zero. |
isFull | bool | True when health is at its maximum. |
Methods
| Method | Description |
|---|---|
virtual void Hurt(DamageData damageData) | Runs the health modifiers, then reduces health by the rounded damage left. Does nothing when dead or when the damage is blocked. |
virtual float Heal(DamageData healData) | Restores healData.damagePoints, up to the maximum. Skips modifiers. Returns the amount actually restored. |
virtual float AffectHealth(float amount, DamageData data) | Changes health by a signed amount (positive heals, negative hurts), clamped between zero and the maximum. Raises OnHealthChanged, and dies at zero. Skips modifiers. Returns the change actually applied. |
virtual void Kill() | Kills immediately. |
virtual void ResetHealth() | Restores health to its maximum and clears the dead state. |
void Spawn(GameObject prefab) | Instantiates a prefab at this object’s position and rotation. |
protected virtual void Die(DamageData killingBlow) | Raises OnDeath once, then destroys the object if _destroyOnDeath is on. Override for custom death handling. |
Examples
using GlowingRifts.ModularHealthSystem;
// Heal 25 from a medkit
health.Heal(new DamageData { damagePoints = 25f, instigator = gameObject });
// Poison that ignores Invincibility and other modifiers
health.AffectHealth(-5f, new DamageData { instigator = gameObject });
// Respawn
health.ResetHealth();Note
Hurt is the only method that runs modifiers. Use it for anything that should respect armor or invincibility.