Steer with a thumbstick or the arrow keys
Updated
A rover drives with the right XR controller’s thumbstick and speeds up with its trigger; outside a headset, where the controller reads as disconnected, the arrow keys and Shift drive it.
Click the scene and drive the rover with the arrow keys; hold Shift to go faster. In a headset, use the right thumbstick and trigger. Drag to orbit the camera, scroll to zoom.
Steer with a thumbstick or the arrow keys
#pragma lifecycle(startup, update)
const SPEED = 2; // metres a secondlet right;let rover;let wasConnected = false;
function startup() { right = Input.xr.controller('right'); rover = findEntity('Rover'); log('XR inputs now:', Input.xr.count(), '· the arrow keys drive until a right controller connects');}
function update(dt) { if (right.connected !== wasConnected) { wasConnected = right.connected; log(wasConnected ? `Right ${right.isHand ? 'hand' : 'controller'} connected` : 'Right controller gone: arrow keys drive'); }
let x = 0; let z = 0; let boost = 1; if (right.connected) { const stick = right.stick(); // pushing up gives a negative y: forward is -z x = stick.x; z = stick.y; boost += right.value(XRButton.Trigger); } else { if (Input.keyboard.isKeyPressed(Key.ArrowLeft)) x -= 1; if (Input.keyboard.isKeyPressed(Key.ArrowRight)) x += 1; if (Input.keyboard.isKeyPressed(Key.ArrowUp)) z -= 1; if (Input.keyboard.isKeyPressed(Key.ArrowDown)) z += 1; if (Input.keyboard.isKeyPressed(Key.ShiftLeft)) boost = 2; } if (x === 0 && z === 0) return;
const step = SPEED * boost * dt; rover.position.x = THREE.MathUtils.clamp(rover.position.x + x * step, -5, 5); rover.position.z = THREE.MathUtils.clamp(rover.position.z + z * step, -5, 5); rover.rotation.y = Math.atan2(-x, -z); // face the way it drives}How it works
Section titled “How it works”The script is explained step by step in Steer with a thumbstick or the arrow keys.
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.
XrStickSteering.xrproj