Skip to main content
Up and Coming
November 4, 2014

Switching between Rift cameras + working example

Edit: Below is my original post. It has since then been solved. Here's a working example.

I have been trying to find a way to switch between two Rift cameras and have yet to find something that works.

I tried modifying this script to work with Rift cameras:

#pragma strict

var cam1 : GameObject;
var cam2 : GameObject;

function Start() { }

function Update() {

if (Input.GetKeyDown(KeyCode.C)) {
cam1.SetActive(true);
cam2.SetActive(false);
}
if (Input.GetKeyDown(KeyCode.V)) {
cam1.SetActive(false);
cam2.SetActive(true);
}


}


It works great for conventional cameras but not with the Rift.

I have two Rift cameras in my scene, one is enabled and one is disabled.
The code itself works, but something along the way breaks.

What happens so far when the script is called:
- cam1 is disabled
- cam2 is enabled
- view goes blank

..and that's where I get stuck.

Not really familiar with the Rift camera's code, so maybe having two of them breaks something? I did notice that the OVRmanager script on the second Rift camera just disappears after the switching script runs.

At the moment, it seems easier to just have a script move the camera to where it should be instead of switching.

Does anyone know why this happens or a way around it?

EDIT:

I tried this code:

#pragma strict

public var ovrcam1 : GameObject;
public var overheadcam : GameObject;
var isoverheadcam : boolean;

function Start () {}

function Update () {

if (Input.GetKeyDown(KeyCode.C))
{
if (!isoverheadcam)
{
isoverheadcam = true;
ovrcam1.SetActive(false);
overheadcam.SetActive(true);
//
} else if(isoverheadcam)
{
ovrcam1.SetActive(true);
overheadcam.SetActive(false);
}

}

}


It lets you switch once, but switching again causes the screen to freeze/go blank.

Still completely lost on why or what is breaking...
    This topic has been closed for replies.

    8 replies

    ezRocketAuthor
    Up and Coming
    November 4, 2014
    New post to keep it seperate from the OP.

    Okay, after going through various threads, I'm just gonna use this for now:
    #pragma strict

    public var ovrcam1 : GameObject;
    public var overheadcam : GameObject;
    var isoverheadcam : boolean;

    function Start () {}

    function Update () {

    if (Input.GetKeyDown(KeyCode.C))
    {
    if (!isoverheadcam)
    {
    isoverheadcam = true;
    overheadcam.SetActive(true);

    } else if(isoverheadcam)
    {
    // set the main cam as true, just in case something messed up or it got disabled somehow
    ovrcam1.SetActive(true);
    overheadcam.SetActive(false);
    isoverheadcam=false;
    }

    }

    }


    It's stupidly simple but seems to be the best workaround at the moment.
    Basically what it does or what happens:

    The main camera stays enabled, it's just the secondary camera that is toggled.

    For this to work, your secondary camera must be higher in the hierarchy than the main camera.
    And... that's it really.
    Inspiring
    November 4, 2014
    Your solution sounds like it will kill performance for your overhead view at some point -- it's still rendering the main camera underneath even if you're not seeing it.

    What if you try "disabling current -> enabling other" in both cases? I've run into this issue as well (starting a few SDK updates ago), but was hoping it was no longer an issue in 0.4.3. My workaround at the time was to deactivate before activating, like this:

        if (Input.GetKeyDown(KeyCode.C)) {
    cam2.SetActive(false);
    cam1.SetActive(true);
    }
    if (Input.GetKeyDown(KeyCode.V)) {
    cam1.SetActive(false);
    cam2.SetActive(true);
    }


    If that doesn't help, maybe try moving the OVRManager out of those prefabs and just have one active one in your scene at all times. I haven't tried this first-hand since I ended up just moving a single camera controller/rig through the scene, re-positioning and re-parenting as needed, so I'm curious to know if you get this to work.
    Titans of Space PLUS for Quest is now available on DrashVR.com
    ezRocketAuthor
    Up and Coming
    November 4, 2014
    Good idea about moving the OVRmanager, gonna try that now.

    EDIT:

    Moved the OVRManager script to a new GameObject, deleted it from the Rift cameras and...
    Well holy shit. It works! Works flawlessly! Awesome. Thanks drash! :D

    Here's the full code I'm using now:


    #pragma strict

    public var ovrcam1 : GameObject;
    public var overheadcam : GameObject;
    var isoverheadcam : boolean;

    function Start () {}

    function Update () {

    if (Input.GetKeyDown(KeyCode.V))
    {
    if (!isoverheadcam)
    {
    isoverheadcam = true;
    ovrcam1.SetActive(false);
    overheadcam.SetActive(true);

    } else if(isoverheadcam)
    {
    ovrcam1.SetActive(true);
    overheadcam.SetActive(false);
    isoverheadcam = false;
    }

    }

    }


    It now correctly de/activates the cameras!
    Inspiring
    November 5, 2014
    Great to know -- thanks so much for following up. :)
    Titans of Space PLUS for Quest is now available on DrashVR.com
    Participant
    November 8, 2014
    On a somewhat related note, if you want to be able to switch between a OVR camera rig and a regular camera (for different builds, perhaps), you have to enable/disable the camera that gets attached to the OVRmanager at runtime (the camera must be on for oculus, off for a normal camera).
    ezRocketAuthor
    Up and Coming
    November 8, 2014
    Here's a small project for those that want to see how it works.

    Nothing big, just a small plane with two cameras, one on the plane and one observing it.

    Pressing V on the keyboard switches between them.
    Cleaned the code up a bit so the variables make more sense.

    It's pretty self-explanatory.

    Open \Assets\_Scenes\main.unity to get started.
    Made with Unity 4.6b21 and SDK 0.4.3

    http://www49.zippyshare.com/v/58936543/file.html
    New Participant
    March 24, 2015
    thank you so much for the example file, you saved me from spending another day trying to figure out how to switch between Oculus cameras without turning them off ! :D
    New Participant
    July 14, 2015
    I guess issue that if you disable OVRCamerRig object after reenabling some coroutines doesn't start cause they are in Start() section in OVRManager.cs
    I just renamed function Start() in OVRManager.cs to OnEnable(). It solved problem for me