Tilt a board, with a keyboard fallback
Updated
A board tilts with the phone through Input.accelerometer; on a desktop, where no reading arrives, the arrow keys tilt it instead.
On a desktop, click the scene and hold the arrow keys. On a phone, press Motion, allow motion access, and tilt the phone. Drag to orbit the camera, scroll to zoom.
Tilt a board, with a keyboard fallback
#pragma lifecycle(startup, update)
const MAX_TILT = DEG_TO_RAD(20);let board;let sensor = false;let x = 0;let z = 0;
function startup() { board = findEntity('Board'); log('Arrow keys tilt the board. On a phone, press Motion and tilt it.');
on('ui/motion', async (event) => { if (event.eventType !== EventType.UIButton.Press) return; const granted = await Input.accelerometer.requestPermission(); log(granted ? 'Motion access granted' : 'Motion access refused'); });}
function update(dt) { let wantX = 0; let wantZ = 0; const g = Input.accelerometer.accelerationIncludingGravity(); if (g) { if (!sensor) log('Tilting with the accelerometer'); sensor = true; // Tilting the phone sideways changes x; tilting it forward or back changes y. wantX = THREE.MathUtils.clamp(g.x / 9.8, -1, 1); wantZ = THREE.MathUtils.clamp(-g.y / 9.8, -1, 1); } else { // No reading: a desktop, or a phone before permission. if (Input.keyboard.isKeyPressed(Key.ArrowLeft)) wantX -= 1; if (Input.keyboard.isKeyPressed(Key.ArrowRight)) wantX += 1; if (Input.keyboard.isKeyPressed(Key.ArrowUp)) wantZ -= 1; if (Input.keyboard.isKeyPressed(Key.ArrowDown)) wantZ += 1; } const ease = Math.min(1, dt * 6); x += (wantX - x) * ease; z += (wantZ - z) * ease; board.rotation.set(z * MAX_TILT, 0, -x * MAX_TILT);}How it works
Section titled “How it works”The script is explained step by step in Tilt a board, with a keyboard fallback.
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.
TiltBoard.xrproj