Personal insights on triggers in Unity


foreword

In Unity 3D, there are two ways to detect collisions, one is to use the collision body, and the other is to use the trigger (Trigger).


1. What is a Trigger?

Triggers are used to trigger events

For example: In a role-playing game, when the player walks to a place, a Boss event will occur, which can be implemented with a trigger. Or when building a portal, a trigger is required to complete the teleportation.

2. Operation steps

1. Check is Trigger

insert image description here

2. Function

Trigger information detection uses the following 3 functions:

  • MonoBehaviour.OnTriggerEnter(Collider collider), fires when the trigger is entered.

  • MonoBehaviour.OnTriggerExit(Collider collider), fired when the trigger is exited.

  • MonoBehaviour.OnTriggerStay(Collider collider), fires when stay in the trigger

Summarize

The difference between a collider and a trigger in Unity 3D is that the collider is the carrier of the trigger, and the trigger is just a property of the collider .

Triggers can be used when you want to detect contact with objects without collision detection affecting object movement, or when you want to detect whether an object passes through a certain area in space . For example, a collision body is suitable for simulating the effect of a car being knocked into the air and a ball falling on the ground and then bouncing up, while a trigger is suitable for simulating the effect of a door automatically opening when a person stands close to the door.

Guess you like

Origin blog.csdn.net/m0_56317546/article/details/123972650