Skip to main content
Version: 20 Mar 2024

Tracking Origin Example

The Magic Leap Reference Space OpenXR Feature allows developers to change the Tracking Origin Mode. This can be done directly on the XR Origin Component or, for greater flexibility, programmatically.

  • Use the Inspector
    • Select the XR Origin GameObject in the Hierarchy and find the XR Origin Component in the Inspector. Select the Tracking Origin Mode property. choose one of the three options: Device, Floor, or Unknown.
  • Use a Script
    • You can also change the Tracking Origin Mode programmatically by referencing the XRInputSubsystem.
    • Note that the XRInputSubsystem must be loaded before you can change the Tracking Origin Mode at runtime.

Example Script

This example script changes the reference space at runtime, after ensuring the XRInputSubsystem is loaded. To test this script, add it to a GameObject and assign an input ToggleAction to toggle between the tracking origin modes.

using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.XR;
using UnityEngine.XR.Management;

public class XRTrackingMode : MonoBehaviour
{
public InputAction ToggleAction;

private XRInputSubsystem _inputSubsystem;

private int _flagIndex = 0;

private readonly TrackingOriginModeFlags[] _supportedFlags =
{ TrackingOriginModeFlags.Device, TrackingOriginModeFlags.Floor, TrackingOriginModeFlags.Unbounded };

IEnumerator Start()
{
yield return new WaitUntil(() => XRGeneralSettings.Instance != null &&
XRGeneralSettings.Instance.Manager != null &&
XRGeneralSettings.Instance.Manager.activeLoader != null &&
XRGeneralSettings.Instance.Manager.activeLoader.GetLoadedSubsystem<XRInputSubsystem>() != null);

_inputSubsystem = XRGeneralSettings.Instance.Manager.activeLoader.GetLoadedSubsystem<XRInputSubsystem>();
Debug.Log("Subsystem ready.");

while (enabled)
{
if(ToggleAction.triggered)
{
_flagIndex = _flagIndex > 2 ? 0 : _flagIndex++;
SetSpace(_supportedFlags[_flagIndex]);
}

yield return null;
}
}

public void SetSpace(TrackingOriginModeFlags flag)
{
if (_inputSubsystem.TrySetTrackingOriginMode(flag))
{
Debug.Log("Current Tracking Mode" + _inputSubsystem.GetTrackingOriginMode());
_inputSubsystem.TryRecenter();
}
else
{
Debug.LogError("SetSpace failed to set Tracking Mode Origin to " + flag);
}
}
}