TriggerTracker
Script that tracks the state of all the triggers that entered the object it is attached to
It also sends the appropriate trigger related callbacks via protected overrideable methods so that the inheriting classes can listen for trigger events they are interested in
Unsurprisingly this script relies on Unity's built-in trigger callbacks (OnTriggerEnter, OnTriggerStay...) in order to function as intended
Previous statement has a specific consequence: In order for TriggerTracker to function as intended it should be attached to a GameObject that has Rigidbody/ArticulationBody or it should be attached to a GameObject that has a Collider component; the former configuration will cause the TriggerTracker to track all the triggers that are activated by any child collider attached to the object's physics body, while the latter will only track the triggers activated by a specific collider
The way the TriggerTracker works makes it completely reliable when it comes to Enter/Exit callbacks, meaning for every OnEnter callback there will always be a corresponding Exit callback sent which is not always the case with Unity's OnTriggerEnter/OnTriggerExit
TriggerTracker's behaviour and trigger callback sending is affected by its own enabled state, if the script is disabled no new triggers will be tracked nor callbacks sent as long as it remains disabled; all tracked triggers are removed and Exit callbacks are sent for each of them when the TriggerTracker gets disabled
Previous statement about TriggerTracker's behaviour means that OnExit callbacks can be sent for all entered triggers even though the triggers have not actually exited the TriggerTracker's object, but this will only happen when the TriggerTracker is disabled or destroyed
TriggerTracker can be viewed as utility script that can be used for tracking entered triggers for any GameObject, but it serves as a base for certain more specific VR related behaviours that are based on enter/exit state of certain triggers
Public members
ReadOnlyCollection<Trigger> Triggers { get; }
A read-only list of all active triggers tracked by the TriggerTracker at any given moment
Elements of this list are of Trigger type, which is a class that contains members which provide some useful info about the object that caused trigger enter event to happen, namely the collider that entered the TriggerTracker's object along with the Rigidbody/ArticulationBody of the incident Collider, if there is one
List<Transform> IgnoreChildCollidersOf { get; }
As the name would suggest, this is a list of Transforms whose child colliders will be ignored by the TriggerTracker, if a Collider that is a child of any of the Transforms in this list enters the TriggerTracker's object, no callbacks are sent by the TriggerTracker and no corresponding entry in the Triggers list is added
This property is also exposed in the TriggerTracker's inspector of the Unity editor
It provides additional mechanism for ignoring certain colliders from affecting a TriggerTracker depending behaviours, in addition to the usual ones provided by the Unity engine such as configuring layers and assigning objects to the appropriate layers, etc.
Protected members
virtual bool ShouldIgnoreCollider(Collider collider)
An overrideable method that is usually called the first when a trigger enters the TriggerTracker's object, it provides the inheriting behaviours with the opportunity to ignore specific objects in addition to assigning objects to the IgnoreChildCollidersOf list, should they need to
Base implementation of this method always returns false
virtual void OnFirstEnter()
This method is called when a trigger has started to be tracked by the TriggerTracker when there were no other triggers tracked at the moment a trigger entered, i.e. Triggers list was empty at the time a trigger entered
virtual void OnLastExit()
This method is the opposite of the OnFirstEnter, it is called after the last trigger that was tracked by the TriggerTracker has exited; to be more specific, when the Triggers list remains empty of tracked triggers
Due to the previously described behaviour of the TriggerTracker, this method will be called if the TriggerTracker is disabled or destroyed, but only if there were any triggers tracked by the TriggerTracker at the same time
virtual void OnEnter(Trigger trigger)
Called whenever an active trigger has started to be tracked by the TriggerTracker, i.e. for every new entry in the Triggers list of the TriggerTracker
This method is called immediately after the OnFirstEnter has been called, in case the trigger specified by this method's parameter is the only entry in the Triggers list
This method would usually be used in the same way the Unity's OnTriggerEnter would be used
virtual void OnExit(Trigger trigger)
The opposite of OnEnter, it is called whenever a trigger exits the TriggerTracker's object, i.e. whenever a trigger is stopped being tracked by the TriggerTracker
If the TriggerTracker is disabled or destroyed this method will be called for every currently tracked trigger
In case the specified trigger was the only remaining entry in the Triggers list of the TriggerTracker, this method will be called immediately before OnLastExit is called
This method would usually be used in the same way the Unity's OnTriggerExit would be used
virtual void OnTriggersUpdated()
This method is called on every occasion the TriggerTracker has evaluated the state of its active tracked triggers, but only in case where there were any active triggers tracked by the TriggerTracker
It is called after all other previously described callbacks in case any of them were called
It will not be called after the OnLastExit, take note of that
void ExitAllTriggers()
The only non-virtual and non-callback method, it can be used by derived classes to explicitly exit all currently tracked triggers
Certain circumstances may require such behaviour by more specific implementations of TriggerTracker, so this method is best used instead of explicity disabling and re-enabling itself
All the appropriate callbacks will be sent
Last updated