Untitled

šŸ“°Getting Started | ā­**Updates |** šŸ“ Guides | šŸ”¢ API | ā“FAQ

Guides Overview

The Proximity API allows functions inĀ BehaviorĀ scripts to be executed as a response to distance-based proximity events on Elements.

Essentially, when you get close (or far away) enough to an object, you can make events occur at that moment using this API.

This API is available viaĀ requireĀ statements.

const proximity = require('proximity');

Distance Calculations

The Proximity API measures whether or not elements are in proximity of others using an internal distance calculation method. When the distance calculated is close enough to the proximity radius you set in the Schema section below, an event will occur.

There are two methods for calculating distance, that are more helpful in different scenarios.

These methods can be toggled with the two functions below. Once either of these functions are called, the Proximity API will calculate distance based on the specified method until it is changed.

The distance method is set globally. Different scripts can not use different distance calculation methods, for example. Once you set it in one script, you will set it for all of them.

const proximity = require('proximity');

// sets proximity to calculate horizontal distance
proximity.useHorizontalDistance();

// sets proximity to calculate horizontal distance
proximity.use3DDistance();

You can call either of these functions in the enter() function of a script to set the distance method you want to use from the moment that script is created.

Subscriptions

Proximity events are dispatched when a Trigger enters a subscribed Element's radius. By default the user is a Trigger, and other Elements can be set as Triggers through their schema. Additionally, a trigger or subscribed Element's radius can be configured through schema editing.

function enter(){
    proximity.subscribe(this, 'enter', onTriggerEnter);
    proximity.subscribe(this, 'exit', onTriggerExit);
}

function onTriggerEnter(){
this.transform.scale = vec3(0.5, 0.5, 0.5);
}

function onTriggerExit(){
this.transform.scale = vec3(1, 1, 1);
}

function exit(){
    proximity.unsubscribe(this, 'enter', onTriggerEnter);
    proximity.unsubscribe(this, 'exit', onTriggerExit);
}

Any Element can be subscribed for a proximity event. Callbacks are invoked with a reference to the Trigger that entered itsĀ innerRadius. Any Trigger that is above or below a subscribed Element in the hierarchy won't invoke events. It is good practice to call subscribe in a script'sĀ enterĀ and unsubscribe inĀ exit.

The proximity callbacks have optional arguments, which contain information on which listener & trigger were responsible for the event.

function onTriggerEnter(listener, trigger) {
  log.info('Proximity Event!');
  log.info('Listener: ' + listener);
  log.info('Trigger: ' + trigger);
}

Schema

All schema configuration is done under theĀ proximityĀ namespace.

// marks an element as a trigger.
this.schema.setNumber('proximity.trigger', true);

// The distance where an element or trigger will dispatch 'enter' events.
this.schema.setNumber('proximity.innerRadius', 0.25);

// The distance where an element or trigger will dispatch 'exit' events.
this.schema.setNumber('proximity.outerRadius', 2);

Module Events

Module Methods

subscribe(element, event, callback)

Subscribes to a proximity event.

unsubscribe(element, event, callback)

Unsubscribes from a proximity event.

useHorizontalDistance()

Sets the Proximity API to trigger proximity events based on your distance from an element on a horizontal plane.

use3DDistance()

Sets the Proximity API to trigger proximity events based on your distance from an element in 3D space.

forceProximityCheck()

Forces an event check regardless of any previous collisions.

Next: Random

Sidebar Table of Contents


Untitled