When using OpenXR in Unity, How do I detect when the HMD is removed

220 Views Asked by At

How do I detect when the user takes off the HMD when I'm using OpenXR in Unity,

I tried using userpresence in the New Input System but the HMD Removed event only fires AFTER the HMD is put back on!!!

Turns out this is a bug and is unusable at the moment. A day of Google searching only presented many people with the same Issue.

1

There are 1 best solutions below

4
David On

After a day of struggle I finally found a solution. You simply poll the velocity of the HMD and when it is Exactly a zero vector the user has taken off the HMD

======================= Edit 1 ===========================

I've only tested this on the Oculus Quest 3 So I don't know if it's a General Solution

======================================================

void Update()
{
    if (HMDFound)
    {
        Vector3 velocity;
        inputDevice.TryGetFeatureValue(CommonUsages.deviceVelocity, out velocity);
        var hmdMounted =  velocity != Vector3.zero;
        if (hmdMounted != lastHmdMounted)
        {
            go.SetActive(hmdMounted);
            OnUserPresence(hmdMounted);
            lastHmdMounted = hmdMounted;
        }
    }
    else
    {
        var inputDevices = new List<InputDevice>();
        InputDevices.GetDevices(inputDevices);

        foreach (var device in inputDevices)
        {
            if (device.characteristics.HasFlag(InputDeviceCharacteristics.HeadMounted))
            {
                inputDevice = device;
                HMDFound    = true;
            }
        }
    }
}