Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Oculus HMD in Unity

Tags:

c#

unity3d

oculus

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.

like image 247
Gigimoi Avatar asked Nov 21 '14 21:11

Gigimoi


People also ask

How do I connect Oculus with Unity?

Go to the Unity Asset Store tab(Click Window > Asset Store) in Unity and search Oculus Integration in the search field at the top. Download and import the Oculus Integration into your project. At the end of the import you may see a prompt that says API update required. Click on “I made a backup, go ahead!”

How do I connect my VR headset to unity?

Enabling Unity VR support To enable VR for your game builds and the editor, open the Player Settings (menu: Edit > Project Settings > Player). Select Other Settings and check the Virtual Reality Supported checkbox. Set this for each build target.


Video Answer


2 Answers

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.

like image 169
Yes Barry Avatar answered Sep 22 '22 14:09

Yes Barry


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

like image 22
ThinkOutside Avatar answered Sep 20 '22 14:09

ThinkOutside