Triggers
Triggers are an essential tool for enabling interactions between objects in 3D space. By setting up trigger events, you can activate desired functionality when two or more objects come into contact with each other.
For example, you can create an event to occur when a 3D model enters a designated zone or when two objects collide with each other.
Adding Triggers
To add a trigger to your project, simply right-click on the Hierarchy and go to the Triggers dropdown. Currently, Mattercraft supports four trigger shapes:
SphereTrigger
PointTrigger
PlaneTrigger
CubeTrigger
Each of these shapes defines the area in which the trigger event will occur.
Using Triggers
Tags
For triggers to interact with each other, they must have the exact same tag assigned to each of them. Tags act as identifiers, meaning that triggers with the same tag will interact with each other while ignoring others with different tags, or those with none assigned at all.
To set the tag for a trigger, you should:
- Navigate to the Properties Panel and find the Other section (near the bottom)
Add the exact same tag name (using any naming convention) to the
Tag
property for all triggers that should interact with each other.Local tags
By default, triggers are set up to fire globally. If you want your trigger to only interact with components in its current zcomp, then you can add a local:
tag. For example local:myTriggerTag
. All subsequent tags should then take this same naming convention.
Is Trigger
Triggers can only emit events when they intersect with another trigger that is marked as a Trigger.
Only one trigger in the interaction needs to be marked as a trigger, typically the one for which you've set up the events.
Trigger events
Triggers emit the following events:
Event | Description |
---|---|
onTriggerEnter |
Fired when the triggers first intersect. |
onTriggerMove |
Fires when the triggers are intersecting and moving. |
onTriggerLeave |
Fired when as soon as a trigger leaves another. |
Triggering events with Behavior Actions
You may trigger events from within the Mattercraft visual editor, or with custom scripts.
You can activate a state by using the following instructions:
- Find the Trigger you wish to trigger events with inside the Hierarchy Panel and select it
- Head to the Behavior Actions Panel and click on the + (plus) icon
- Assign the desired Behavior Action and the trigger’s
Event
property
Triggering events with a Script
You can also listen for trigger events in a script. Below is an example of activating a state with the onTriggerEnter
event handler:
// Register that the trigger node should be listening
// to an event, in this case onTriggerEnter.
this.register(this.instance.onTriggerEnter, evt => {
// Activate the state by finding the layer containing the
// state clip, and then the state itself
this.zcomponent.animation.layers.myLayer.clips.myState.play();
});
See Mattercraft's full API documentation here.