Skip to content

XR controllers and hands

Updated

In a headset, the player’s hands are the controllers they hold, or their bare hands when the device tracks them. Input.xr gives a Behavior script both: each controller’s buttons, analog trigger and thumbstick, where it is and where it points, and for tracked hands every finger joint, how bent each finger is, and gestures such as a pinch, a fist or a swipe. This page covers the controller object, the XRButton constants, poses, hand tracking and gestures.

For what the headset knows of the viewer and the room (the head pose, the passthrough camera, the room’s light and the tracked body), see XR input.

You should know:

  • Pressed versus released, and polling in update; see Keyboard, mouse and touch.
  • What an immersive session is and how the player rig works; see XR.
  • Vectors and quaternions: THREE.Vector3 and THREE.Quaternion are available to every script.

Input.xr.controller(ref) returns the controller in a hand, 'left' or 'right', or the XR input at an index from 0 to Input.xr.count() - 1.

  • The same object every time. It is always live: keep it in a variable from startup and ask it every frame.
  • Empty when absent. While nothing is there (outside a session, or the controller is switched off), connected is false and every query reads as untouched: buttons up, stick centred, poses null.
  • Hands and controllers alike. When the player puts the controllers down and the device tracks their hands, the same calls read the hands. XRButton.Trigger is then the pinch.

Input.xr.count() is how many XR inputs there are now: controllers, tracked hands, and screen taps on an AR phone. It is 0 outside XR.

Member What it returns
connected Whether a controller or tracked hand is there this frame.
isHand Whether it is a tracked hand rather than a controller.
handedness 'left', 'right' or 'none'; null when not connected.
isPressed(button) Whether button is held.
isReleased(button) true for one frame after button was let go.
isPressedFor(button, seconds) While held, true once every seconds, like the keyboard’s isKeyPressedFor.
value(button) How far button is pushed, 0 to 1: analog for Trigger and Squeeze, 0 or 1 for the others.
stick() The thumbstick as { x, y }, each from -1 to 1. Pushing up gives a negative y. On controllers without a stick, the touchpad.
position(space?) Where it is in the world, a new THREE.Vector3; null without a pose.
orientation(space?) How it is turned, a new THREE.Quaternion; null without a pose.
direction() Where it points, a new unit vector; null without a pose.
joint(name) A tracked hand’s joint (see Hand joints), or null.
gesture(name) The phase of a gesture this frame (see Gestures), or null.
curl(finger) How bent a finger is, 0 straight to 1 curled; null for a controller or a hand not fully in sight.

XRButton names the buttons:

Constant Value Button
XRButton.Trigger 0 The index-finger trigger. On a tracked hand, the pinch.
XRButton.Squeeze 1 The grip button under the middle finger.
XRButton.Touchpad 2 The touchpad, on controllers that have one.
XRButton.Thumbstick 3 Pressing the thumbstick in.
XRButton.A and XRButton.X 4 The lower face button: A on the right controller, X on the left.
XRButton.B and XRButton.Y 5 The upper face button: B on the right controller, Y on the left.

A and X share a value because they are the same button position on each hand: ask the right controller for A and the left one for X.

A pose is a position and an orientation together. Every controller has two:

  • 'ray' (the default): where the controller points from, the origin of its laser.
  • 'grip': where the hand holds it, for attaching a held object.

Poses are in world space and follow the player rig like the camera, so they stay right after a teleport. Every query returns new objects, so you can keep or change them freely.

joint(name) takes a joint name from the WebXR hand model, such as 'wrist', 'thumb-tip', 'index-finger-tip' or 'middle-finger-metacarpal', and returns { position, orientation, radius }: where the joint is in the world, how it is turned, and the radius of the finger at that joint, in metres. It returns null for a controller, or for a joint the device cannot see.

curl(finger) says how bent a finger is, from 0 (straight) to 1 (curled into the palm). The fingers are 'thumb', 'index', 'middle', 'ring' and 'pinky'.

gesture(name) returns the phase of a gesture this frame:

Phase Meaning
'start' The first frame of the gesture.
'hold' Each frame it continues.
'end' The frame it is let go.
null The gesture is not happening.

There are two families:

  • Hand shapes, tracked hands only: 'pinch', 'fist', 'thumbs-up', 'point' and 'open'. A shape must be made clearly to start and let go of clearly to end, so it does not flicker on and off.
  • Swipes, hands and controllers: 'swipe-left', 'swipe-right', 'swipe-up' and 'swipe-down', as the player sees them. A swipe is a move of at least 20 cm in under 0.3 s, mostly in one direction. It is 'start' in the frame it happens and 'end' in the next, never 'hold'. A hand making a fist or a pinch does not swipe.

1. Steer with a thumbstick or the arrow keys

Section titled “1. Steer with a thumbstick or the arrow keys”

A rover drives with the right controller’s thumbstick, and the trigger speeds it up. Outside a headset the controller reads as disconnected, so the arrow keys and Shift drive it instead.

StickSteering.jx
#pragma lifecycle(startup, update)
const SPEED = 2; // metres a second
let 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
}
  • Input.xr.controller('right') is fetched once: it is the same live object every frame, connected or not.
  • The script logs when connected changes, so you can see a controller or a tracked hand arrive in a headset.
  • stick().y is negative when pushed away from the player, which is the scene’s forward (-z) here, so it is used as it is.
  • value(XRButton.Trigger) is analog: half a pull gives half the boost.
PointAndSelect.jx
#pragma lifecycle(startup, update)
let right;
function startup() {
right = Input.xr.controller('right');
}
function update() {
if (!right.connected || !right.isReleased(XRButton.Trigger)) return;
const ray = GraphicsRaycaster.fromController(right);
const hit = ray && ray.hit();
if (hit && hit.entity) {
log(`Selected ${hit.entity.name}`);
emit('xr/select', { id: hit.entity.entityId });
Haptics.vibrate(40, { hand: 'right', intensity: 0.5 });
}
}
  • The controller object is fetched once and kept; it stays live for the whole script.
  • isReleased(XRButton.Trigger) fires once per trigger pull, when the trigger is let go. On a tracked hand, the trigger is the pinch, so the same script works with bare hands.
  • GraphicsRaycaster.fromController builds a ray along where the controller points. It returns null while the controller is not tracked, hence the ray && guard.
  • Haptics.vibrate gives a short buzz in the right hand to confirm the selection; see Device and haptics.

2. Steer with the thumbstick, scale with the grip

Section titled “2. Steer with the thumbstick, scale with the grip”
StickAndSqueeze.jx
#pragma lifecycle(startup, update)
const SPEED = 1.5; // metres a second at full push
let left;
let right;
let balloon;
function startup() {
left = Input.xr.controller('left');
right = Input.xr.controller('right');
balloon = findEntity('Balloon');
}
function update(dt) {
// Left stick: move the balloon on the ground plane. Up on the stick is negative y.
const stick = left.stick();
balloon.position.x += stick.x * SPEED * dt;
balloon.position.z += stick.y * SPEED * dt;
// Right grip: inflate from 1x to 2x as it is squeezed.
const squeeze = right.value(XRButton.Squeeze);
balloon.scale.setScalar(1 + squeeze);
// Right A button: back to the start.
if (right.isReleased(XRButton.A)) balloon.position.set(0, 1.2, -1);
}
  • stick() returns { x, y } from -1 to 1. Because pushing up gives a negative y, adding it to z moves the balloon away from the player (negative Z is forward).
  • A disconnected controller reads as a centred stick, so no connected check is needed for the movement.
  • value(XRButton.Squeeze) is analog: half a squeeze gives 0.5, so the balloon grows smoothly with the grip.
  • XRButton.A is asked of the right controller, where the A button is.

3. Gestures: swipe through slides, thumbs-up to like

Section titled “3. Gestures: swipe through slides, thumbs-up to like”
GestureControls.jx
#pragma lifecycle(startup, update)
let hands;
function startup() {
hands = [Input.xr.controller('left'), Input.xr.controller('right')];
}
function update() {
for (const hand of hands) {
if (!hand.connected) continue;
if (hand.gesture('swipe-left') === 'start') emit('slides/next');
if (hand.gesture('swipe-right') === 'start') emit('slides/previous');
if (hand.isHand && hand.gesture('thumbs-up') === 'start') {
emit('reaction/like', { hand: hand.handedness });
}
if (hand.isHand && hand.gesture('fist') === 'end') emit('hand/release');
}
}
  • Both hands are kept in an array so the same checks run for each.
  • Swipes work with controllers and hands. Comparing with 'start' reacts once per swipe.
  • Hand shapes such as 'thumbs-up' and 'fist' exist only for tracked hands, so the script checks isHand first. 'end' reacts in the frame the fist opens.
  • handedness tells listeners which hand made the gesture.

4. A fingertip cursor and a point-to-press

Section titled “4. A fingertip cursor and a point-to-press”
FingertipCursor.jx
#pragma lifecycle(startup, update)
let right;
let dot;
let pointing = false;
function startup() {
right = Input.xr.controller('right');
dot = findEntity('Fingertip Dot');
dot.visible = false;
}
function update() {
const tip = right.isHand ? right.joint('index-finger-tip') : null;
dot.visible = Boolean(tip);
if (!tip) return;
dot.position.copy(tip.position);
dot.scale.setScalar(tip.radius * 2);
// Pointing: index straight, middle finger curled.
const index = right.curl('index');
const middle = right.curl('middle');
const nowPointing = index !== null && middle !== null && index < 0.2 && middle > 0.7;
if (nowPointing && !pointing) emit('hand/point-start');
pointing = nowPointing;
}
  • joint('index-finger-tip') gives the fingertip’s world position and its radius. It is null for a controller or while the fingertip is out of sight, so the dot hides then.
  • The dot is scaled to the finger’s thickness (radius * 2 is its diameter).
  • curl reads each finger’s bend. A straight index with a curled middle finger is a pointing hand. The built-in 'point' gesture does the same job; reading curls lets you define your own shapes.
  • pointing remembers the previous frame, so the signal is emitted once when pointing begins.
ChargedShot.jx
#pragma lifecycle(startup, update)
let right;
let charge = 0;
function startup() {
right = Input.xr.controller('right');
}
function update(dt) {
if (right.isPressed(XRButton.Trigger)) {
charge = Math.min(1, charge + dt);
if (right.isPressedFor(XRButton.Trigger, 0.2)) {
Haptics.vibrate(30, { hand: 'right', intensity: charge });
}
}
if (right.isReleased(XRButton.Trigger) && charge > 0) {
const from = right.position();
const dir = right.direction();
if (from && dir) emit('weapon/shoot', { from, dir, power: charge });
charge = 0;
}
}
  • While the trigger is held, charge climbs from 0 to 1 over a second.
  • isPressedFor(XRButton.Trigger, 0.2) is true every fifth of a second while held, so a short buzz repeats, stronger as the charge grows.
  • On release, the shot starts from the controller’s ray position() and travels along direction(). Both are null without a pose, so they are checked before use.
  • Reading y as up. stick().y is negative when the stick is pushed up (away from the player). Flip it if you need “up is positive”.
  • Asking the wrong hand for a face button. XRButton.A and XRButton.X share a value. Ask the right controller for A and B, the left one for X and Y.
  • Using poses without checking for null. position(), orientation(), direction() and joint() return null while there is no tracking. Check before calling methods on the result.
  • Expecting hand shapes from a controller. 'pinch', 'fist', 'thumbs-up', 'point' and 'open' exist only for tracked hands. Swipes work with both.
  • Expecting count() to be 1 or 2 outside XR. It is 0 outside a session. Code that should also run on desktop must handle the controller being disconnected.
  • Writing position on a physics body to attach it to a hand. Use a Grabbable instead: it welds the object to the hand or controller and drives its rigid body correctly; see Selection and grabbing.