XR Origin
Updated
In an immersive session the player stands in a real room, and the headset tracks where they are in it. The player rig carries that room through your scene. The XR Origin component decides how the rig sits on the spawn point and lets the player recenter: bring the scene back in front of them, wherever they have wandered in the room. This page covers the home placement, the XR Origin’s props, its methods and events, and how it relates to spawn points and the Character Controller.
Before you start
Section titled “Before you start”- Read the XR overview for the ideas of session, player rig and home.
- The scene has a Spawn Point, which places the player when they join. See Spawn points.
- You can read XR controller buttons with
Input.xr.controller. See XR controllers and hands.
The home placement
Section titled “The home placement”The spawn point always places the player, in VR and AR alike. Where it puts the rig at the start of the session is the home.
- When a session starts, the runtime draws again from the active spawn points, even if the player has already spawned and moved on the desktop.
- Without a spawn point, the home is the scene’s origin.
- A scene loaded additively moves no one.
- Spawn points, spot points, travel points and the VR teleport move the rig during the session. With a Character Controller on the main camera, they move its body, and the rig follows.
The XR Origin builds on the home:
- Recenter puts the user’s head on the home, facing its way, from wherever they stand in the room. The rig moves and turns about the vertical under them.
- Reset puts the room’s origin back on the home, undoing recenters and movement.
- Device recenter. When the device recenters its own tracking (for example a long press of the headset’s menu button), the XR Origin sends
XROrigin::resetand goes back to the home.
Where it lives
Section titled “Where it lives”The XR Origin (comp-xr-origin) goes on the XR World entity, which starts empty: add it there. To receive its events, add a Signal Emitter to the XR World too.
| Prop | Type | Default | Description |
|---|---|---|---|
floorOffset |
number | 0 |
Metres the room is raised at the spawn placement, or lowered below 0. Use it for seated play, so a seated player’s eyes are at standing height in the scene. |
worldScale |
number | 1 |
The player’s size relative to the scene: above 1 a giant, below 1 tiny. |
Both apply at every spawn placement. Editing a prop during a session applies it at once, as a reset() does.
Scripting
Section titled “Scripting”| Member | Description |
|---|---|
recenter() |
Put the user’s head on the home, facing its way. Returns false when the head is not tracked or there is no session. Sends XROrigin::recenter. |
reset() |
Put the room’s origin back on the home. Returns the home’s yaw, in radians. Does nothing outside a session. |
origin |
The player rig in a session, a THREE.; undefined outside one. |
Find the component once, in startup:
const xrOrigin = findEntity('XR World').getComponent(XROriginComponent);Events
Section titled “Events”With a Signal Emitter on the XR World:
| eventType | Constant | data | When |
|---|---|---|---|
XROrigin::recenter |
Event |
{} |
recenter() put the user on the home. |
XROrigin::reset |
Event |
{} |
The device recentered its tracking, and the rig went back to the home. |
Examples
Section titled “Examples”1. A recenter button with feedback
Section titled “1. A recenter button with feedback”A UI button emits ui/recenter. The player feels a short vibration when the recenter happens.
#pragma lifecycle(startup)
function startup() { const xrOrigin = findEntity('XR World').getComponent(XROriginComponent);
on('ui/recenter', () => xrOrigin.recenter());
on('xr-world', (event) => { if (event.eventType === EventType.XROrigin.Recenter) { Haptics.vibrate(60, { intensity: 0.5 }); } if (event.eventType === EventType.XROrigin.Reset) { log('The headset recentered its tracking'); } });}A UI Button recenters the XR Origin, and the controllers buzz when the XR Origin reports the recenter; a reset from the headset is logged.
Press Recenter. On a desktop there is no XR session to recenter; in a headset the view snaps back to the home placement and the controllers buzz.
- The first listener answers a plain signal, sent by a UI button or another script.
- The second listener receives the XR Origin’s events through the XR World’s Signal Emitter.
Haptics.vibrate(60, …)buzzes both controllers for 60 milliseconds. See Device and haptics.XROrigin::resetcomes from the device itself, for example after the player held the headset’s button; the rig is already back on the home.
Snippets
Section titled “Snippets”1. Recenter with the left grip
Section titled “1. Recenter with the left grip”#pragma lifecycle(startup, update)
let xrOrigin;let left;
function startup() { xrOrigin = findEntity('XR World').getComponent(XROriginComponent); left = Input.xr.controller('left');}
function update() { if (left.isReleased(XRButton.Squeeze)) { if (!xrOrigin.recenter()) log('Cannot recenter: head not tracked'); }}- The XR Origin and the left controller are looked up once.
Input.xr.controller('left')returns the same live object every frame, so keeping it is safe. isReleased(XRButton.Squeeze)is true for one frame after the grip button is let go, so the action runs once per press.recenter()returnsfalseoutside a session or while tracking is lost; the script says so instead of failing silently.
2. Seated mode
Section titled “2. Seated mode”A menu choice switches between standing and seated play by raising the room.
#pragma lifecycle(startup)
const SEATED_RAISE = 0.5; // metres: about the difference between sitting and standing eye height
function startup() { const xrOrigin = findEntity('XR World').getComponent(XROriginComponent);
on('settings/seated', (data) => { const seated = Boolean(data && data.seated); xrOrigin.setProps({ floorOffset: seated ? SEATED_RAISE : 0 }); log(seated ? 'Seated play' : 'Standing play'); });}floorOffsetraises the room at the spawn placement, so a seated player’s eyes are at standing height in the scene.- Changing it with
setPropsduring a session applies it at once, like areset(): the player is put back on the home with the new height. - Outside a session the new value is kept and used at the next entry.
3. Become a giant
Section titled “3. Become a giant”Pressing the right thumbstick toggles between normal size and a giant’s view of a miniature city.
#pragma lifecycle(startup, update)
let xrOrigin;let right;let giant = false;
function startup() { xrOrigin = findEntity('XR World').getComponent(XROriginComponent); right = Input.xr.controller('right');}
function update() { if (!right.isReleased(XRButton.Thumbstick)) return; giant = !giant; xrOrigin.setProps({ worldScale: giant ? 10 : 1 });}XRButton.Thumbstickis the click of the thumbstick.worldScale: 10makes the player ten times their size, so the city around them looks like a model on a table.- The edit applies at once and re-places the player on the home at the new scale.
4. Measure how far the player has wandered
Section titled “4. Measure how far the player has wandered”The rig and the viewer’s pose tell how far the player has walked from where the session started.
#pragma lifecycle(startup, update)
const LIMIT = 3; // metresconst home = new THREE.Vector3();let xrOrigin;let warned = false;let homeKnown = false;
function startup() { xrOrigin = findEntity('XR World').getComponent(XROriginComponent);}
function update() { const rig = xrOrigin.origin; const viewer = Input.xr.pose; if (!rig || !viewer.tracked) { homeKnown = false; return; } if (!homeKnown) { home.copy(viewer.origin); homeKnown = true; }
const far = viewer.origin.distanceTo(home) > LIMIT; if (far !== warned) { warned = far; emit(far ? 'hud/warn-boundary' : 'hud/clear-warning'); }}originis the player rig during a session andundefinedoutside one, which makes it a simple “am I in a session?” test here.Input.xr.pose.originis where the viewer’s head is in the world this frame. The first tracked position is kept as the starting point.- The HUD is told only when the player crosses the limit, not every frame.
- See XR input for the viewer’s pose.
Common mistakes
Section titled “Common mistakes”- Putting the XR Origin elsewhere. It belongs on the XR World entity.
- No spawn point. Without one, the home is the scene’s origin, which may be inside a wall.
- Expecting
recenter()to work on the desktop. It needs a session and a tracked head, and returnsfalseotherwise. - Changing
worldScaleorfloorOffsetevery frame. Each edit re-places the player. Change them on a choice, not continuously. - Moving the rig yourself. Use spawn points, spot points, the teleport or a Character Controller to move the player, and
recenter()orreset()to restore the home. - Listening without a Signal Emitter. The XR Origin’s events reach scripts only through a Signal Emitter on the XR World.