A knob that sets a value
Updated
A script sets a knob’s Handle to turn only about Y, within about 275 degrees, and maps the angle to a volume from 0 to 1 when it is let go.
Drag across the knob to turn it, then let go: the volume is sent on audio/volume. Drag elsewhere to orbit the camera, scroll to zoom.
A knob that sets a value
#pragma lifecycle(startup)
const MIN_ANGLE = -2.4; // radians, turned fully clockwise (seen from above)const MAX_ANGLE = 2.4; // turned fully counter-clockwise
function startup() { // The Knob has a Handle and a Signal Emitter with the signal 'knob'. const knob = findEntity('Knob'); knob.getComponent(HandleComponent).setProps({ translate: 'as-rotate', rotate: { x: false, y: [MIN_ANGLE, MAX_ANGLE], z: false }, });
on('knob', (event) => { if (event.eventType === EventType.Handle.Hover) Haptics.vibrate(10, { intensity: 0.2 }); if (event.eventType !== EventType.Handle.End) return;
// Turning clockwise lowers rotation.y: fully counter-clockwise is 0, fully clockwise is 1. const volume = (MAX_ANGLE - knob.rotation.y) / (MAX_ANGLE - MIN_ANGLE); emit('audio/volume', { value: Math.min(1, Math.max(0, volume)) }); });}How it works
Section titled “How it works”The script is explained step by step in A knob that sets a value.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
VolumeKnob.xrproj