Skip to main content

Locomotion

In XR, users walk around their room, and the headset moves the camera with them. To take them further than their room allows, move the camera's parent, the rig. The headset moves the camera within the rig, and the rig carries the camera, the controllers and the hands through the scene.

This page covers moving the rig yourself, and the engine's XrNavigation script, which lets users teleport, move and turn with their controllers and hands.

Moving the Rig​

Move the rig to move the user, and rotate it about the vertical axis to turn them. The user's head is rarely above the rig's origin, as they move around their room, so:

  • To teleport the user to a point, move the rig so that their head, rather than the rig's origin, ends up above the point.
  • To turn the user, rotate the rig about their head, rather than about its origin, so that they turn on the spot.
// Move the user so that their head is above a point on the floor
const teleportTo = (point) => {
const head = camera.getPosition();
const origin = rig.getPosition();
rig.setPosition(point.x - (head.x - origin.x), point.y, point.z - (head.z - origin.z));
};

// Turn the user about their head, by an angle in degrees
const headBefore = new Vec3();
const turn = (degrees) => {
headBefore.copy(camera.getPosition());
rig.rotate(0, degrees, 0);
rig.translate(headBefore.sub(camera.getPosition()));
};

These assume a local-floor reference space, in which the rig's origin is on the floor, and a rig with no parent and no scale.

To put the user somewhere when a session starts, place the camera there before it starts. The XrSession script moves the rig to the ground below the camera, facing the same way.

The XrNavigation Script​

The XrNavigation script offers several ways to move at once, so that users can choose the ones they're comfortable with:

InputWhat it does
Hold select (the trigger, or a pinch), aim, and let goTeleports. While the user aims, an arc shows where they would land, in red where they can't
Left thumbstickMoves smoothly, relative to the way the user faces
Right thumbstick, left or rightTurns, by 45° each time the user pushes it, or smoothly
Right thumbstick, forward or backRises or sinks 0.5 meters each time the user pushes it, or 2 meters while they hold the right grip

Hands have no thumbsticks, so with hands, users teleport by pinching. The script ignores the pinches that open the system menu on a Meta Quest.

Add the script to the rig, before the session starts:

import { XrNavigation } from 'playcanvas/scripts/esm/xr/xr-navigation.mjs';

rig.script.create(XrNavigation, {
properties: {
turnMode: 'smooth',
enableSnapVertical: false
}
});

The camera must be a child of the rig, as the script moves the user relative to the camera. Its main attributes:

AttributeDefaultDescription
enableTeleporttrueTeleport with select
enableMovetrueMove with the left thumbstick, and turn with the right one
movementSpeed1.5The speed of thumbstick movement, in meters per second
turnMode'snap''snap' to turn in steps, 'smooth' to turn while the thumbstick is held, or 'none'
rotateSpeed45The angle of each snap turn, in degrees
smoothTurnSpeed90The speed of smooth turning, in degrees per second
enableSnapVerticaltrueRise and sink with the right thumbstick
maxTeleportDistance10The furthest a teleport can go, in meters
groundHeight0The height of the flat ground that teleports land on
teleportArcSpeed8The speed the arc is thrown at. A faster arc reaches further
validTeleportColor, invalidTeleportColorPale blue, redThe colors of the arc where the user can and can't land

Other attributes set the height of each vertical step, the thumbsticks' thresholds, and the width, detail and landing ring of the arc.

Landing on Your Geometry​

By default, teleports land on a flat plane at groundHeight. For uneven ground, steps or platforms, set the script's castRay function. The script calls it with the start and end of each segment of the arc in world space, in turn from the hand outward, and the arc lands at the first point it returns. With physics, cast each segment into the physics world:

const navigation = rig.script.xrNavigation;
const from = new Vec3();
const lastEnd = new Vec3();
let blocked = false;

navigation.castRay = (start, end) => {
// Each segment starts where the last one ended, so a segment that doesn't starts a new arc
if (!start.equals(lastEnd)) blocked = false;
lastEnd.copy(end);
if (blocked) return null;

// Cast from 5 cm before the segment, as a ray that starts on a surface can miss it
from.sub2(start, end).normalize().mulScalar(0.05).add(start);
const result = app.systems.rigidbody.raycastFirst(from, end);
if (!result) return null;

// Land on surfaces that face up, such as floors and steps
if (result.normal.y > 0.7) return result.point;

// Anything else, such as a wall, stops the arc
blocked = true;
return null;
};

castRay replaces the flat plane. When it returns null, the arc carries on to its next segment, so an arc that meets a wall would carry on through it, and land behind it. That's why the function above remembers that the arc has hit a wall, and returns null for the rest of it. To let users land on some surfaces only, filter the ray cast by collision group or tag. castRay is a function, which can't be a script attribute, so set it from code in the Editor, React and Web Components too.

When your code moves the rig onto another floor itself, set the script's currentGroundHeight to that floor's height. Teleports keep the user's height above the floor they stand on.

Teleporting and Menus​

The script teleports whenever a select ends with the arc on valid ground. That includes a select that clicks a button or a menu item, so turn teleporting off while users interact with the interface:

const navigation = rig.script.xrNavigation;
let menuOpen = false;

// XrMenu fires this as its menu opens and closes
app.on('xr:menu:active', (active) => {
menuOpen = active;
});

app.on('update', () => {
// No teleporting while a menu is open, or a ray points at an element
const pointingAtUi = app.xr.input.inputSources.some((inputSource) => inputSource.elementEntity);
navigation.enableTeleport = !menuOpen && !pointingAtUi;
});

elementEntity is the entity of the element that an input source points at, or null. See UI in XR.

Moving the World​

Rather than moving the user through the scene, you can let them move the scene. The XrManipulation script lets users grab the scene with both hands to move, turn and scale it, which suits models and maps that they look at from every side.

Comfort​

When users see themselves move while their body feels still, some of them feel sick. These guidelines keep movement comfortable for most people:

  • Prefer teleporting and snap turns. They show no movement, so they suit nearly everyone. Offer smooth movement and turning as options for users who like them. XrNavigation moves smoothly with the left thumbstick unless enableMove is false, and turns smoothly only when turnMode is 'smooth'.
  • Move only when the user asks. Don't move or turn the rig on your own, for example to show the user something. If you must, fade the view to black, move, and fade it back in.
  • Keep speeds steady. Changes of speed are less comfortable than speed itself. Start and stop smooth movement at once, at a moderate speed, such as the default of 1.5 meters per second.
  • Keep the horizon level. Turn the rig only about the vertical axis, and never roll or pitch it.
  • Keep the frame rate up. Dropped frames make any movement worse. See Performance.

See Also​