Search Unity

Simply detecting the oculus rift's presence [SOLVED]

Discussion in 'AR/VR (XR) Discussion' started by drhmiri, Jan 28, 2015.

  1. drhmiri

    drhmiri

    Joined:
    Jun 17, 2014
    Posts:
    82
    Hi all,

    Could you please help me understand why the first 'if' clause works but the second one returns nothing?

    Thanks

    Code (CSharp):
    1.     void Start ()
    2.     {
    3.         if ( Ovr.Hmd.Detect() > 0 )
    4.             Debug.Log ( "Rift detected!" );
    5.  
    6.         if ( OVRManager.display.isPresent && OVRManager.tracker.isPresent )
    7.             Debug.Log ( "Both display and tracker are present!" );
    8.     }
     
    rdvt likes this.
  2. akabane

    akabane

    Joined:
    Apr 26, 2007
    Posts:
    143
    I can only try and give you a guess, but my idea would be that display.isPresent and tracker.isPresent get initialized and set to true after the Start() finishes.
    So when you call Start, it checks the Ovr.hmd.detect which could just be if a rift is indeed connected (which it is in your case), but then goes to initialize the display and tracker, and even if that initialization takes a millisecond more than the Start frame, your second "if" will never become true. To check if that's indeed the reason, you can try doing something like:
    Code (CSharp):
    1. void Start ()
    2.     {
    3.         if ( Ovr.Hmd.Detect() > 0 )
    4.             Debug.Log ( "Rift detected!" );
    5.        StartCoroutine(MoreChecks());
    6.     }
    7.  
    8. IEnumerator MoreChecks()
    9. {
    10. while ( !OVRManager.display.isPresent && !OVRManager.tracker.isPresent )
    11.             { Debug.Log ( "Still not detected" ); }
    12. Debug.Log("Both display.isPresent and tracker.isPresent = true");
    13. }
    Haven't tested this but the idea is to hang inside a while until both bools become true, if this will ever print "Both display.isPresent and tracker.isPresent = true" to your console that means that, as I suggested, the Rift initializations for display and tracker are not done to be ready "on start".

    IF that's the case, then you could easily go and make sure all your scripts are synchronized to accomodate for that time offset needed to initialize tracker and display.ispresent.

    Keet updating as I'm curious too!
     
    drhmiri likes this.
  3. drhmiri

    drhmiri

    Joined:
    Jun 17, 2014
    Posts:
    82
    Thank you very much. I learned a lot from your post already...

    I incorporated your suggestion, which first generated an error message that I had never come across before:

    Code (CSharp):
    1. void Start()
    2. {
    3.     if ( Ovr.Hmd.Detect() > 0 )
    4.         Debug.Log ( "Rift Mode" );
    5.     else
    6.         Debug.Log ( "Normal Mode" );
    7.  
    8.     StartCoroutine( MoreOculusChecks() );
    9. }
    Code (CSharp):
    1. public IEnumerator MoreOculusChecks()
    2. {
    3.     while ( !OVRManager.display.isPresent && !OVRManager.tracker.isPresent )
    4.     {
    5.         Debug.Log ( "Still not detected" );
    6.     }
    7.  
    8.     Debug.Log( "Both display.isPresent and tracker.isPresent are now true!" );
    9. }

    This was the error message: error CS0161: `myScript.MoreOculusChecks()': not all code paths return a value

    I added a line to address this, based on what I learned about this err msg. Not too sure if this is the best way though. Also, not sure if this method should be static as well?

    Code (CSharp):
    1. public IEnumerator MoreOculusChecks()
    2. {
    3.     while ( !OVRManager.display.isPresent && !OVRManager.tracker.isPresent )
    4.     {
    5.         Debug.Log ( "Still not detected" );
    6.     }
    7.  
    8.     Debug.Log( "Both display.isPresent and tracker.isPresent are now true!" );
    9.  
    10.     yield return null;
    11. }
    But I still only get the msg from Ovr.Hmd.Detect() in the console.

    I am working on it, but please do indulge me with your ideas...

    Thank you,
     
  4. akabane

    akabane

    Joined:
    Apr 26, 2007
    Posts:
    143
    Try to change MoreOculusChecks() into this

    Code (CSharp):
    1. bool displaytracker_initialized;
    2.  
    3. bool MoreOculusChecks()
    4. {
    5.     if ( !OVRManager.display.isPresent && !OVRManager.tracker.isPresent )
    6.     {
    7.         Debug.Log("still not detected");
    8.         return false;
    9.     }
    10.    else
    11.    {
    12.      Debug.Log( "Both display.isPresent and tracker.isPresent are now true!" );
    13.      return true;
    14.     }
    15. }
    16.  
    And then, inside your Update(), put this somewhere:

    Code (CSharp):
    1.  
    2. displaytracker_initialized = MoreOculusChecks();
    this will run the check every frame and will additionally give you a variable (displaytracker_initialized) that is either true or false based on the result of the check
     
    drhmiri likes this.
  5. drhmiri

    drhmiri

    Joined:
    Jun 17, 2014
    Posts:
    82
    Many thanks . .

    This works, as I get a deluge of Both display.isPresent and tracker.isPresent are now true! messages for every frame once it start running!

    Cheers
     
  6. akabane

    akabane

    Joined:
    Apr 26, 2007
    Posts:
    143
    Good, is there ANY time you get the "still not detected" message?
    It would be good to pinpoint at what point they both become true :)

    (Anyway, just make sure everything is detected to stop calling that function :p )
     
  7. drhmiri

    drhmiri

    Joined:
    Jun 17, 2014
    Posts:
    82
    .

    The still not detected message never appears with that code!

    The HMD must be initialized right after Start() I guess... :confused:

    .
     
  8. Jason-H

    Jason-H

    Joined:
    Nov 5, 2010
    Posts:
    87
    In version 5.2.2p1 Unity provides it's own functionality to detect whether a VR device is detected.

    It doesn't require the OVRManager to be present and can therefore be used with other VR headsets.

    First you need to declare that you're using the VR namespace at the top of your script:
    Code (CSharp):
    1. using UnityEngine.VR;
    And then you can use this bool to check whether it's detected:
    Code (CSharp):
    1. if (VRDevice.isPresent)
    See: http://docs.unity3d.com/ScriptReference/VR.VRDevice-isPresent.html

    Versions earlier than the patch version 5.2.2p1 also have this functionality but it is broken and always returns true. In this case, if you do not want to install a patch you can use:
    Code (CSharp):
    1. if (VRSettings.loadedDevice != VRDeviceType.None)
     
    Last edited: Nov 4, 2015
  9. Akusan

    Akusan

    Joined:
    Aug 24, 2014
    Posts:
    12
    Will this identify whether a Gear VR or Oculus Rift is used?
     
  10. Jason-H

    Jason-H

    Joined:
    Nov 5, 2010
    Posts:
    87
    If you are using either of the devices then both of these options will tell you whether one is connected.

    If you are trying to distinguish which device is being used between the two of them then you could first check whether your build target is Android or desktop and then use the previously mentioned enum checks.

    VRSettings.loadedDevice returns as VRDeviceType.Oculus for both GearVR and Oculus but if you've checked which build target you're aiming for then you'll know which device is connected.
     
  11. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    With the new (now official; ONLY supported; XR toolkit) ... isPresent appears to be broken in Unity 2019.x (all versions through to 2019.3.3).

    XRSettings.isPresent == always returns false, no matter what
    XRSettings.isDeviceActive == correctly returns true when VR headset is plugged in and live.
     
    JoeStrout likes this.