Skip to content

PhysicsEventRelay

Broadcasts 3D and 2D physics callbacks (collision/trigger enter/exit) as subscribable events.

Namespace: Jungle.Physics
Inherits: MonoBehaviour

Overview

Add PhysicsEventRelay to any GameObject with a Collider that needs physics event monitoring. It converts Unity's OnCollisionEnter, OnTriggerEnter, etc. into C# events that pass the other GameObject involved in the interaction. Listeners apply their own filtering.

Events

Event Fires When
CollisionEntered OnCollisionEnter (3D)
CollisionExited OnCollisionExit (3D)
TriggerEntered OnTriggerEnter (3D)
TriggerExited OnTriggerExit (3D)
CollisionEntered2D OnCollisionEnter2D
CollisionExited2D OnCollisionExit2D
TriggerEntered2D OnTriggerEnter2D
TriggerExited2D OnTriggerExit2D

All events are Action<GameObject>.

Methods

Method Description
Register(PhysicsEventType, Action<GameObject>) Subscribe to a specific physics event type.
Unregister(PhysicsEventType, Action<GameObject>) Unsubscribe from a physics event type.
FlushExits() Fires synthetic Exit events for every collider currently tracked as overlapping (3D + 2D, trigger + collision), then clears tracking. Reentrant.

Stranded-exit protection

Unity does not fire OnTriggerExit or OnCollisionExit when one of this GameObject's own colliders is disabled, has isTrigger flipped, or is destroyed while still overlapping — listeners would be stuck thinking the other object is still inside.

The relay tracks every overlapping other-collider on enter/exit and exposes FlushExits(), which synthesizes the missing exits. It's called automatically from OnDisable (covers GameObject deactivation and relay/GameObject destruction).

For programmatic collider mutation, the SetEnabled and SetTrigger collider operations call FlushExits() before applying their change, so the bug never surfaces through Jungle commands. If you toggle collider.enabled or collider.isTrigger from your own code on a GameObject that has a relay, call FlushExits() manually first.

See Also