š°Getting Started | ā**Updates |** š Guides | š¢ API | āFAQ
Web Editor Basics
HoloLens
Mobile
Meta Quest
Chapter Series Documentation
Assets
Scripting
Enklu Embedded
API Reference
Release Notes
FAQ
Contact
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');
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.
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);
}
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);
enter
Ā - Dispatched when a Trigger first enters a subscribed Element'sĀ innerRadius
..stay
Ā - Dispatched every frame after a Trigger enters a subscribed Element'sĀ innerRadius
, but hasn't exited itsĀ outerRadius
.exit
Ā - Dispatched when a Trigger exits a subscribed Element'sĀ outerRadius
.subscribe(element, event, callback)
element <Element>
Ā The element to listen for events applied to it.event <string>
Ā The event to subscribe to.callback <function>
Ā The function invoked when the event occurs.Subscribes to a proximity event.
unsubscribe(element, event, callback)
element <Element>
Ā The element that will be unsubscribed.event <string>
Ā The event to unsubscribe from.callback <function>
Ā The callback to unsubscribe.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