Trigger volumes

Trigger volumes is a type of volume that you can place within your game world to detect when other objects or characters enter or exit it. Trigger volumes are commonly used in games for various purposes, such as triggering events or activating gameplay mechanics.

Creating trigger volumes

TriggerVolumeActor

A trigger volume actor can be placed as an object in your scene. You can find it in the asset browser under actors. You can then refer to the trigger volume actor by adding it as a parameter to another actor.

TriggerVolumeComponent

A trigger volume can be created by attaching a TriggerVolumeComponent to an actor. The trigger volume is a box so you need to provide the dimensions of it.

Handling trigger volume events

Whenever an actor enters or exists a trigger volume, you can trigger some functionality. In the example below, every time the Character actor overlaps with a Coin actor, it increases the number of collected coins bye one and then removes the coin actor from the world.

@Actor()
class Character extends BaseActor {
    private physicsSystem = inject(PhysicsSystem)
    private world = inject(World)
    
    collectedCoins: number = 0

    onInit() {
        this.physicsSystem.onBeginOverlapWithActorType(this, Coin)
            .subscribe(coin => {
                this.collectedCoins++
                world.remove(coin)                
            })
    }
}

@Actor()
class Coin extends BaseActor {
    private triggerVolume = attach(TriggerVolumeComponent, { 
        dimensions: new Vector3(1, 1, 1) 
    })
}

Last updated