Simply detecting the oculus rift's presence [SOLVED]

Hi all,

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

Thanks

    void Start ()
    {
        if ( Ovr.Hmd.Detect() > 0 )
            Debug.Log ( "Rift detected!" );

        if ( OVRManager.display.isPresent && OVRManager.tracker.isPresent )
            Debug.Log ( "Both display and tracker are present!" );
    }
1 Like

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:

void Start ()
    {
        if ( Ovr.Hmd.Detect() > 0 )
            Debug.Log ( "Rift detected!" );
       StartCoroutine(MoreChecks());
    }

IEnumerator MoreChecks()
{ 
while ( !OVRManager.display.isPresent && !OVRManager.tracker.isPresent )
            { Debug.Log ( "Still not detected" ); }
Debug.Log("Both display.isPresent and tracker.isPresent = true");
}

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!

1 Like

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:

void Start()
{
    if ( Ovr.Hmd.Detect() > 0 )
        Debug.Log ( "Rift Mode" );
    else
        Debug.Log ( "Normal Mode" );

    StartCoroutine( MoreOculusChecks() );
}
public IEnumerator MoreOculusChecks()
{
    while ( !OVRManager.display.isPresent && !OVRManager.tracker.isPresent )
    {
        Debug.Log ( "Still not detected" );
    }

    Debug.Log( "Both display.isPresent and tracker.isPresent are now true!" );
}

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?

public IEnumerator MoreOculusChecks()
{
    while ( !OVRManager.display.isPresent && !OVRManager.tracker.isPresent )
    {
        Debug.Log ( "Still not detected" );
    }

    Debug.Log( "Both display.isPresent and tracker.isPresent are now true!" );

    yield return null;
}

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,

Try to change MoreOculusChecks() into this

bool displaytracker_initialized;

bool MoreOculusChecks()
{
    if ( !OVRManager.display.isPresent && !OVRManager.tracker.isPresent )
    {
        Debug.Log("still not detected");
        return false;
    }
   else
   {
     Debug.Log( "Both display.isPresent and tracker.isPresent are now true!" );
     return true;
    }
}

And then, inside your Update(), put this somewhere:

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

1 Like

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

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 :slight_smile:

(Anyway, just make sure everything is detected to stop calling that function :stuck_out_tongue: )

.

The still not detected message never appears with that code!

The HMD must be initialized right after Start() I guess… :face_with_spiral_eyes:

.

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:

using UnityEngine.VR;

And then you can use this bool to check whether it’s detected:

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:

if (VRSettings.loadedDevice != VRDeviceType.None)
3 Likes

Will this identify whether a Gear VR or Oculus Rift is used?

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.

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.

1 Like