Pickup

Pickup is the abstract base for items collected on touch. It needs a trigger collider and is destroyed once collected.

Members

MemberDescription
OnCollectedUnityEvent<GameObject> invoked when collected, with the collector (the collider’s Rigidbody GameObject when it has one).
bool TryCollect(Collider other)Collects the pickup if Apply accepts the collider. Returns true if collected. Called automatically from OnTriggerEnter.
protected abstract bool Apply(Collider other)Applies the pickup’s effect. Return false to ignore the collider.
protected virtual void OnTriggerEnter(Collider other)Calls TryCollect. Override to collect from somewhere else, e.g. a raycast.

A pickup is only collected once. After OnCollected, the GameObject is destroyed.

Example: Health Pack

using UnityEngine;
using GlowingRifts.ModularHealthSystem;

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

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

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

Tip

Returning false when health is full leaves the pack in the world for later.