5
public Transform OculusPlayerPrefab;
public Transform DefaultPlayerPrefab;
void Start() {
    Transform player = OVRDevice.IsHMDPresent() ?
        (Transform)Instantiate(OculusPlayerPrefab) :
        (Transform)Instantiate(DefaultPlayerPrefab);
    player.position = transform.position;
}

This should detect if the oculus rift HMD is connected and instantiate the oculus player prefab, otherwise the default. However, IsHMDPresent() returns false whether the Oculus Rift is connected or not. In the unity/oculus integration package however, OVRMainMenu uses the IsHMDPresent() method with the expected results.

Gigimoi
  • 125
  • 2
  • 11
  • Hi Gigimoi, A quick question about your code: This basically means that if your oculus prefab is instantiated, you do not actually need to drag the prefab manually and place it in the Hierarchy panel. Right? –  Jan 28 '15 at 10:14

3 Answers3

2

Edit: this answer is from 2014 and based on Unity 4. You probably want to use the other answers.

I found this method to be working best:

Ovr.Hmd.Detect() > 0

Also remember of the the HMDLost/HMDAcquired events, so you don't have to poll this every frame:

bool oculusPresent=false;
void CheckOculusPresence() {
  oculusPresent=Ovr.Hmd.Detect() > 0;
}

void Start() {
  CheckOculusPresence();
  OVRManager.HMDAcquired+=CheckOculusPresence;
  OVRManager.HMDLost+=CheckOculusPresence;
}

(oculus SDK 0.4.3/unity3d 4.5.5, OSX/Windows)

Krzysztof Bociurko
  • 4,575
  • 2
  • 26
  • 44
  • Hi guys, I used: if ( Ovr.Hmd.Detect() > 0 ) Debug.Log ( "Rift detected . ." ); and it works fine, but when I use: if ( OVRManager.display.isPresent && OVRManager.tracker.isPresent ) Debug.Log ( "Both display and tracker are present!" ); I get nothing... Any idea why? Thanks –  Jan 27 '15 at 14:18
  • @Jtech, did your method have any advantage over mine? Also, what version of SDK did you test this on? – Krzysztof Bociurko Jan 27 '15 at 14:33
  • Hi, I am using this on Oculus Version 1.5 (SDK 0.4.4) with Unity Version 4.6.1f1 but I can only use what you suggested (Ovr.Hmd.Detect() > 0) so the other one I came across (OVRManager.display.isPresent && OVRManager.tracker.isPresent) is not actually returning anything, which I don't know why! Could you try this please and see if you can get it to return something? –  Jan 27 '15 at 17:09
  • Haven't got an rift with me right now, but on 0.4.3 this was the most sure method i have found - why won't you use it? – Krzysztof Bociurko Jan 27 '15 at 17:31
  • I am using (Ovr.Hmd.Detect() > 0) but I am also wondering why the other one (OVRManager.display.isPresent && OVRManager.tracker.isPresent) is not returning anything? –  Jan 28 '15 at 09:43
  • @Jtech I am using the mobile SDK and isPresent is not working here, either. ChanibaL: probably because the documentation points toward isPresent being the "proper" way to do it. I will try this method, though. – Abandoned Cart Mar 05 '15 at 14:12
2

Unity now has a built-in way to detect this.

http://forum.unity3d.com/threads/simply-detecting-the-oculus-rifts-presence-solved.294089/#post-2368233

Docs: http://docs.unity3d.com/ScriptReference/VR.VRDevice-isPresent.html

ThinkOutside
  • 103
  • 10
  • While this may answer the question, [it would be preferable](http://meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – IKavanagh Nov 04 '15 at 12:26
2

As of (at least) Unity 2018.2, using the Oculus Utilities, the following works:

if (OVRManager.isHMDPresent) {
    // headset connected
}

I'll add that you can subscribe to HMDMounted and HMDUnmounted events as well which is somewhat related:

OVRManager.HMDMounted   += MyOnHMDMountedFunction();
OVRManager.HMDUnmounted += MyOnHMDUnmountedFunction();

Those will fire when you put on (HMDMounted) and/or take off (HMDUnmounted) your headset.

Yes Barry
  • 9,514
  • 5
  • 50
  • 69