Search Unity

Resolved Is there any way to detect the headset is taken off?

Discussion in 'VR' started by sonoxx56, Oct 2, 2021.

  1. sonoxx56

    sonoxx56

    Joined:
    Dec 15, 2019
    Posts:
    7
    I want to make the game goes back to main screen 3 seconds later after the player take off the headset.

    Maybe detecting whether the camera rotating will work..? (Still, don;t know how to detect that...)
    I'm beginner so it's hard to find which way is effective. If you have any advice, please let me know.
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,043
    If you use the new input system you should be able to make an action for HMD removed from head
     
  3. sonoxx56

    sonoxx56

    Joined:
    Dec 15, 2019
    Posts:
    7
    Sadly I don't use the new input system ... Should I? Looks like I don't even know what I need to know. :0
     
  4. sonoxx56

    sonoxx56

    Joined:
    Dec 15, 2019
    Posts:
    7
  5. djhatvr

    djhatvr

    Joined:
    Sep 22, 2019
    Posts:
    53
    The analogy to this solution for OpenXR does not work because of a bug in which you don't receive the HMD removed event until AFTER the headset is put back on!

    But 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

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (HMDFound)
    4.         {
    5.             Vector3 velocity;
    6.             inputDevice.TryGetFeatureValue(CommonUsages.deviceVelocity, out velocity);
    7.             var hmdMounted =  velocity != Vector3.zero;
    8.             if (hmdMounted != lastHmdMounted)
    9.             {
    10.                 go.SetActive(hmdMounted);
    11.                 OnUserPresence(hmdMounted);
    12.                 lastHmdMounted = hmdMounted;
    13.             }
    14.         }
    15.         else
    16.         {
    17.             var inputDevices = new List<InputDevice>();
    18.             InputDevices.GetDevices(inputDevices);
    19.  
    20.             foreach (var device in inputDevices)
    21.             {
    22.                 if (device.characteristics.HasFlag(InputDeviceCharacteristics.HeadMounted))
    23.                 {
    24.                     inputDevice = device;
    25.                     HMDFound    = true;
    26.                 }
    27.             }
    28.         }
    29.     }
     
    mgear likes this.