OnTriggerEnter()
OnTriggerStay()
OnTriggerExit()
You can make a Collider into a Trigger in order to use the OnTriggerEnter()
, OnTriggerStay()
and OnTriggerExit()
methods. A Trigger Collider will not physically react to collisions, other GameObjects simply pass through it. They are useful for detecting when another GameObject is in a certain area or not, for example, when collecting an item, we may want to be able to just run through it but detect when this happens.
The method below is an example of a trigger listener that detects when another collider enters the collider of a GameObject (such as a player). Trigger methods can be added to any script that is assigned to a GameObject.
void OnTriggerEnter(Collider other)
{
//Check collider for specific properties (Such as tag=item or has component=item)
}