Physics Events
When colliding, the collider will produce physical a behavior, however the trigger will not. Therefore, the trigger only performs collision detection. The collider performs both collision detection and physical simulation. The difference between them:
A Trigger is a Collider component whose property is true
. When a collision occurs, the Trigger does not produce collision effect, so the Trigger is only used for collision detection.
The differences between Trigger and Collider are as follows:
- Trigger do not perform finer detection with other triggers or colliders.
Trigger Events and Collision Events
The differences between Trigger events and Collision events are as follows:
- Trigger events are generated by triggers, and collision events are generated based on collision data.
- The trigger event can be generated by the trigger with another trigger or another collider.
- Collision events need to be generated by two colliders and at least one dynamic rigid body.
Where the collision pairs that can generate trigger events are:
Listen to trigger events
In order to add listeners to the trigger event, you need to add the corresponding callback by registering the event:
- Get Collider through
this.getComponent(Collider)
- Register the callback of the corresponding event through the on or once method of Collider
Code example:
Collision Events
Collision events are divided into three types:
Where the collision pairs that can generate collision events are:
Listen to collision events
In order to add a listener to the collision event, you need to add the corresponding callback by registering the event:
- Get Collider through
this.getComponent(Collider)
- Register the callback of the corresponding event through the on or once method of Collider
let collider = this.getComponent(Collider);
}
private onCollision (event: ICollisionEvent) {
console.log(event.type, event);
}