3D Physics Events

3D Physics Events are divided into trigger and collision events, which are generated by Trigger and Collider respectively.

Trigger and Collider

A Trigger is a Collider component whose Is Trigger property is true. When a collision occurs, the Trigger does not produce collision effect, so the Trigger is only used for collision detection.

A Collider is a Collider component whose Is Trigger property is false. When a collision occurs, the Collider will produce collision effect. So the Collider can not only perform collision detection, but also produce physical effects.

The differences between Trigger and Collider are as follows:

  • Trigger do not perform finer detection with other triggers or colliders.
  • Collider do more detailed detection with other colliders, and will provide some additional data due to collisions, such as collision points, normals and so on.

Trigger and Collision Events

The Trigger Event can be generated by one Trigger and another Trigger or another Collider. The Collision Event can only be generated by two Colliders.

Trigger Event

There are three types of Trigger Event:

Events Event explanation
trigger-enter Start trigger
trigger-stay Keep trigger
trigger-exit End trigger

Listen to trigger event

You can listen to the trigger event by registering events, the steps are as follows:

  1. Get Collider3D through this.getComponent(cc.Collider3D).
  2. Then use the on or once method of Collider3D to register the event callback.

Code example:

  start () {
      let collider = this.getComponent(cc.Collider3D);
      collider.on('trigger-stay', this.onTrigger, this);
  },

  onTrigger (event) {
      console.log(event.type, event);
  }

Note: Collider3D is the base class for all Collider components in the physics system.

Collision Event

There are three types of Collision Event:

Event Event explanation
collision-enter Start collision
collision-stay Keep collision
collision-exit End collision

Listen to collision event

You can add callbacks after collision by registering events. The steps are as follows:

  1. Get Collider3D through this.getComponent(cc.Collider3D).
  2. Then use the on or once method of Collider3D to register the event callback.

Code example:

  start () {
      let collider = this.getComponent(cc.Collider3D);
      collider.on('collision-stay', this.onCollision, this);
  },

  onCollision (event) {
      console.log(event.type, event);
  }

results matching ""

    No results matching ""