Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question How to properly check if VR is activated at runtime ?

Discussion in 'VR' started by Yaos, Feb 14, 2022.

  1. Yaos

    Yaos

    Joined:
    Mar 26, 2015
    Posts:
    6
    Hey!
    I'm a bit of a coding noob. I use Bolt and if it isn't enough I use scripts to return variables I can use in Bolt.

    Recently I've been trying to detect if VR is on and return a true/false variable based on that (to adjust camera and controls)

    I've been using this code :

    Code (CSharp):
    1. using UnityEngine;
    2. using Ludiq;
    3. using Bolt;
    4. using UnityEngine.XR;
    5.  
    6. public class VRdetection : MonoBehaviour
    7. {
    8.     public bool VRisOn;
    9.    
    10.     void Update()
    11.     {
    12.         VRisOn = VR_On();
    13.     }
    14.  
    15.     private bool VR_On()
    16.     {
    17.         if (XRDevice.userPresence == UserPresenceState.Present)
    18.             return true;
    19.         else
    20.             return false;
    21.     }
    22. }
    23.  
    Which does work sometimes, but most of the time it doesn't detect my headset activity as being there and return false.

    Is there a more reliable way to detect if VR is activated ?

    (I am on Unity 2018.4.15f1 and in my XR settings I have both Oculus SDB and Dash Support check in, as well as OpenVR.)
     
  2. nostraverse

    nostraverse

    Joined:
    Mar 3, 2021
    Posts:
    7
    For me thsi works fine:

    Code (CSharp):
    1. if (UnityEngine.XR.XRSettings.enabled)
    2.  
    I do not know if that helps. Good luck.
     
    Yaos likes this.
  3. Yaos

    Yaos

    Joined:
    Mar 26, 2015
    Posts:
    6
    Well that did it. Somehow while looking through numerous thread I never found this command.

    Thanks a lot!