Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
Welby's avatar
Welby
Adventurer
10 years ago

Simple Script to Switch between VR Mode/Standard Mode

https://www.mediafire.com/?wzz87elmuq4tg48

Well.. since it seems to be something that a lot of people want to do but not everyone knows how to do it..

I've decided to attach here a really simple script to switch in-game between the OVRCameraRig and a standard Camera.

A lot of people are encountering issues related to the OVRManager,so when you try to switch between the two Cameras the editor freezes.

With this script and a little trick you should be able to switch between them without issue.

1) Remove the OVRManager from the OVRCameraRig and attach it to an external object(like a random cube or what you want).

2) Put the Standard Camera in the same position of the OVRCamera Rig,then attach my script to the OVRPlayerController(Or FPS Controller or your main controller..)

3) There are just 3 public variables on my script. Standard Camera,VR Camera and OVR Manager,so just move your "Main camera" into the Standard Camera field,your OVRCameraRig into VR Camera field.. and the object with the OVRManager script attached into the OVR Manager field.

Everything is now ready to work..

Just use "H" on the keyboard to switch between Standard Mode and VR Mode. If you want change key to switch between the modes,just change

if (Input.GetKeyDown (KeyCode.H) && Check==false){ 17th line


and replace the key you want to use instead of "H" after KeyCode.

and do the same here

else if (Input.GetKeyDown (KeyCode.H) && Check==true){  23th line


Hope that helps.. sorry to have explained everything in this "noob" way but i guess it will be more helpful like that for some people.

Also I'm sorry in advance if the script could be better written or even simple to use..

5 Replies

Replies have been turned off for this discussion
  • Thanks for sharing.

    I've been trying to get this in the SDK forever. It's pretty important, I will bring it up again next week.
  • "welby" wrote:
    1) Remove the OVRManager from the OVRCameraRig and attach it to an external object(like a random cube or what you want).


    I don't think that step is necessary. I've been able to switch from VR to 2D with both of those components on the same GameObject, at least with a simple single camera setup.

    Things get dicey quickly as soon as you start adding more cameras in my experience though.

    EDIT: Actually, I think part of the crash issue does not have to do with multiple cameras. Have you tried setting up a simple scene where the VR camera rig is initially disabled? That seems to be the main issue for me. Even in a simple one camera scene, I can get the editor to crash if the VR camera GameObject is initially disabled. There's some sort of initialization that's not happening otherwise.

    So a possible workaround if you want to start with 2D cameras may be to go ahead and have both 2D and VR cameras active on startup, and then immediately disable the one you don't want.
  • "SteveTack" wrote:
    "welby" wrote:
    1) Remove the OVRManager from the OVRCameraRig and attach it to an external object(like a random cube or what you want).


    I don't think that step is necessary. I've been able to switch from VR to 2D with both of those components on the same GameObject, at least with a simple single camera setup.

    Things get dicey quickly as soon as you start adding more cameras in my experience though.

    EDIT: Actually, I think part of the crash issue does not have to do with multiple cameras. Have you tried setting up a simple scene where the VR camera rig is initially disabled? That seems to be the main issue for me. Even in a simple one camera scene, I can get the editor to crash if the VR camera GameObject is initially disabled. There's some sort of initialization that's not happening otherwise.

    So a possible workaround if you want to start with 2D cameras may be to go ahead and have both 2D and VR cameras active on startup, and then immediately disable the one you don't want.


    Actually if i do not remove the OVRManager to another object .. and i try to disable the OVRCameraRig during run-time.. the screen just freezes. Also that's what tboone from oculus team has suggested to do in some thread on this forum and infact this works for me.

    If you disable the OVRCamerarig to switch to the 2D camera.. if you want comeback to the OVRCameraRig a get a freeze screen,instead if you move to another object the OVRManager that doesn't happen.
  • "welby" wrote:
    Actually if i do not remove the OVRManager to another object .. and i try to disable the OVRCameraRig during run-time.. the screen just freezes. Also that's what tboone from oculus team has suggested to do in some thread on this forum and infact this works for me.

    If you disable the OVRCamerarig to switch to the 2D camera.. if you want comeback to the OVRCameraRig a get a freeze screen,instead if you move to another object the OVRManager that doesn't happen.


    For whatever reason, I did not need to move OVRManager (worked without doing that in two separate projects).

    I got switching working in my complex game, but for me it involved letting the VR camera stuff finish its initialization. It seems that if you disable OVRManager before it finishes it's thing, bad stuff happens.

    Anyway, I've posted the details and a Unity project in a thread I started back in January:
    https://forums.oculus.com/viewtopic.php?f=37&t=19117&p=256578#p256578
  • Matt_MD's avatar
    Matt_MD
    Honored Guest
    I've had this working for a while in Unity 4 - we were able to switch between using a standard camera and the OVRCameraRig prefab with no problems at all.

    However, after upgrading to Unity 5, we've been having all sorts of trouble. Mainly to do with the DirectToRift build (this is the one we need), and when running it, any camera that isn't the OVR one just displays as a black screen.

    Has anyone encountered this before / got a solution?