Health

Health Component

The Health component allows any object to receive and process damage. It must be paired with a Hitbox to function fully in the damage system.

Note

Health is part of the Modular Health System, in the GlowingRifts.ModularHealthSystem namespace.

Summary

This component:

  • Stores current and max health
  • Lets health modifiers change or block incoming damage
  • Triggers events when health changes and on death
  • Destroys the object when health reaches zero, unless disabled

Fields

FieldTypeDescription
_healthfloatStarting health of the entity. Set via inspector.
_destroyOnDeathboolDestroy the GameObject when health reaches zero.
OnHealthChangedUnityEvent<float>Invoked when health changes, passing a normalized value from 0 to 1.
OnDeathUnityEvent<DamageData>Invoked once when health reaches zero, with the killing damage (DamageData.Empty when killed through Kill()).

Properties

PropertyTypeDescription
maxHealthfloatMaximum health, captured from the starting health on Awake.
currentHealthfloatCurrent health.
isDeadboolTrue once health has reached zero.
isFullboolTrue when health is at its maximum.

Public Methods

MethodDescription
virtual void Hurt(DamageData damageData)Runs the health modifiers, then reduces health through AffectHealth.
virtual float Heal(DamageData healData)Restores healData.damagePoints health, 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. Invokes OnHealthChanged and dies at zero. Skips modifiers. Returns the change actually applied.
virtual void Kill()Kills the entity immediately.
virtual void ResetHealth()Restores health to its maximum and clears the dead state.

Health Modifiers

Health is a Modifiable. Components deriving from HealthModifier on the same GameObject hook into it while enabled. On every hit, each modifier’s ApplyModifier returns the damage to apply instead; 0 or less blocks it.

using GlowingRifts.ModularHealthSystem;

public class Armor : HealthModifier
{
    [SerializeField] private float _reduction = 0.5f;

    public override float ApplyModifier(DamageData damage)
    {
        return damage.damagePoints * (1f - _reduction);
    }
}

Built-in modifier: Invincibility (blocks all damage while enabled).

Tip

To show floating numbers, use a Hitpoint component: ShowDamage(DamageData) shows the damage at the hit point (hook it to a hitbox’s onHit), and ShowText(string) shows any text, for example "+25" when a health pickup is collected.

Pickups

Pickup is the base for items collected on touch. It needs a trigger collider, raises OnCollected with the collector’s GameObject, and is destroyed once collected. Derive from it and implement Apply; return false to ignore a collider.

using GlowingRifts.ModularHealthSystem;

public class HealthPickup : Pickup
{
    [SerializeField] private float _amount = 25f;

    protected override bool Apply(Collider other)
    {
        Health health = other.GetComponentInParent<Health>();
        if (!health || health.isFull) return false;

        health.Heal(new DamageData { damagePoints = _amount, instigator = gameObject });
        return true;
    }
}

Contact Damage

ContactDamage hurts any Hitbox that enters its trigger, by a set Damage. Use it for spikes, lava or enemies that hurt on touch.

Usage

Tip

Attach this script to the root object of any prefab you want to be damageable.

Note

Use Hitbox components on colliders to forward damage here.

Warning

Damage must be applied through Hurt() to affect health.

See Also