Skip to content

Instantly share code, notes, and snippets.

@SnugglePilot
Created August 26, 2020 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SnugglePilot/278f0a779c4bffea34c258c7beb9c08c to your computer and use it in GitHub Desktop.
Save SnugglePilot/278f0a779c4bffea34c258c7beb9c08c to your computer and use it in GitHub Desktop.
Sets the tracking mode of the XR subsystems. This adds functionality that isn't in the core Unity interface to set XR hardware tracking modes - eg, Oculus devices will track based on initial headset position, wheras OpenVR devices will track based on floor position. This allows you to normalize this across all devices.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
namespace absurdjoy.axe
{
public class SetTrackingMode : MonoBehaviour
{
public TrackingOriginModeFlags trackingFlags = TrackingOriginModeFlags.Floor;
public bool recenterOnSet = true;
private void OnEnable()
{
SetTrackingModeTo(); // Change tracking mode on whatever is already initialized.
SubsystemManager.reloadSubsytemsCompleted += SetTrackingModeTo; // Add listener for future inits.
}
private void OnDisable()
{
SubsystemManager.reloadSubsytemsCompleted -= SetTrackingModeTo;
}
/// <summary>
/// Sets the tracking mode of all currently initialized XR Subsystems to the origin configured in the inspector.
/// </summary>
public void SetTrackingModeTo()
{
SetTrackingModeTo(trackingFlags, recenterOnSet);
}
/// <summary>
/// Sets the tracking mode of all currently initialized XR Subsystems to the origin specified.
/// </summary>
public void SetTrackingModeTo(TrackingOriginModeFlags flags, bool recenter) {
List<XRInputSubsystem> subsystems = new List<XRInputSubsystem>();
SubsystemManager.GetInstances(subsystems);
foreach (var subsystem in subsystems)
{
if (subsystem.TrySetTrackingOriginMode(flags))
{
if (recenter)
{
// Now that we've changed the origin mode we'll have to recalibrate to this new point
// in space (it will re-zero off of the last-set-height, which for Oculus Quest is the
// HMD boot position).
subsystem.TryRecenter();
}
}
else
{
Debug.LogError("Failed to set the TrackingOriginMode of id:"+subsystem.SubsystemDescriptor.id, gameObject);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment