Skip to main content

Physics Triggers

Overview

Physics triggers allow for the detection of objects moving in and out within a specific shape range and getting callbacks to implement the game logic.

Click Create Game Object -> Physics to select and place these objects. Click Add Component -> Scene in the Inspector panel to select the related components to be added.

The game objects and components that currently support the Physics Trigger features are as follows:

image-20230202100521286

Object NameDescription
Box TriggerIt comes with a box component after placement and turns on the trigger feature automatically.
Sphere TriggerIt comes with a sphere component after placement and turns on the trigger feature automatically.
Capsule TriggerIt comes with a capsule component after placement and turns on the trigger feature automatically.

image-20230202100608735

Component NameDescription
Box ComponentIt detects objects moving in and out within the box shape range and generates trigger events.
Sphere ComponentIt detects objects moving in and out within the sphere shape range and generates trigger events.
Capsule ComponentIt detects objects moving in and out within the capsule shape range and generates trigger events.

Note: Triggers do not trigger events from each other.

Additionally, the Collision Enabled property of the component that enters the trigger range must be PhysicsOnly or Query And Physics for the event to be successfully triggered.

Component Properties

物理属性面板

PropertyDescription
TriggerWhether to enable the Physics Trigger feature.
When Trigger is true, the Trigger feature of the component will be enabled.
The default properties of triggers created with LBoxTrigger、LSphereTrigger、LCapsuleTrigger are true and not visible in the Editor.

Trigger Collision Presets

Collision Preset is the Engine's built-in collision preset type specific to physics trigger collision settings.

Click Config -> Physics -> Collision Preset to open the edit panel.

image-20230202095453600

image-20230202094040835

Component NameDescription
Preset NameTrigger
Collision EnablePhysics Only
ChannelTrigger
Channel TypeDefault Collision Response
World StaticIgnore
World DynamicOverlap
PawnOverlap
VehicleOverlap
DestructibleOverlap
VisibilityOverlap
CameraOverlap
TriggerOverlap

Trigger Event Collision Determination Mechanism

  • The channel types of both collision objects are cross-compared with the collision response, and the event will be successfully triggered when there is no Ignore result.
  • The event will not be triggered if the IgnoreTrigger property is true when the component is in range.
Trigger Channel Collision Response With Other ObjectsOther Object Channels Respond To Trigger CollisionsTrigger Event
Block/Overlap+Block/Overlap=Yes
Ignore+Any=No
Any+Ignore=No

Implementing Event Triggering Logic With Script Components

Component Name
Script Component

Component Property

脚本组件

PropertyDescription
Script FileScript File Path

Script Implementation

trigger.lua

function on_begin_play(script_component)
--param[in] script_component Script component object
local owner = script_component.GameObjectOwner
--Bind the current script to owner
nx_bind_script(owner, nx_current())
--Registering trigger events
nx_callback(owner, "on_trigger", "on_trigger")
end

function on_trigger(trigger_object, trigger_component, enter_object, enter_component, enter)
--param[in] trigger_object Trigger game object
--param[in] trigger_component Trigger component
--param[in] enter_object The game object that enters the trigger range
--param[in] enter_component The component object that enters the trigger range
--param[in] enter In or out of trigger range
end

Creating Convex Triggers

A rigid body file (.nxx) with a convex collision as the trigger type can be created and assigned to the Rigid File property of the model component to generate a convex trigger.

A rigid file can be imported or created as described in the Rigidbody Editor documentation or the Creating Colliders documentation.