Skip to main content
Up and Coming
July 25, 2013

Maintain Orientation from level to level in Unity?

Every time I load a new level the Oculus treats whatever position I happen to be in as the new center. What is the best way to overcome this? I need Untiy to not reset the camera position every time. I've tried a few ideas, but none of them seem like the best practice.
    This topic has been closed for replies.

    13 replies

    Up and Coming
    January 22, 2014
    You could just take all the init/shutdown code out of OVRDevice and call it from somewhere else. There is no reason why it has to be tied to any Unity object lifecycle or scene, they are just C calls.

    Rip all the code out of Awake and OnDestroy and put it in your own Setup/Shutdown methods. Just make sure your setup is called before the first Awake cycle finishes. Having it initialize more than once per game execution is wasteful anyway.

    The really correct way to do this on Oculus' end would be to only shutdown when the game is actually quitting, using OnApplicationQuit or something. Then on awake, query the sdk to see if it is already initialized. Leaks would be possible if you intentionally destroy the OVRDevice before quitting, but otherwise the behavior would be correct. Maybe add an editor update that shuts down the sdk if its active outside of play mode to be safe. Its unfortunate that Unity has no "global" script hooks to tie behaviors to engine events, instead of only to gameobjects.
    New Participant
    August 4, 2015
    basically I create a game object, put it into the VR cam and reset its transform every frame
    that way the last correct transform will be saved (assuming small true movements)
    so 1 frame after level loaded (oculus stuff always happens at the end of the frame as it seems) the gameobject will be taken out of the camera, the Game object the camera lies in will be put into the game Object
    the after reset of the transform vise versa.

    so put this script onto a game object containing the OVR camera and somehow tell the script, when a level is loaded and all will be fine :D




    /*
    credits to benjamin kiesewetter 2015
    www.benjaminkiesewetter.de
    */

    using UnityEngine;
    using System.Collections;


    public class OculusLevelOffsetStabilizer : MonoBehaviour {



    public GameObject OVRCaameraRig; //THE OCULUS CAMERA RIG, needs to be a child of this game object
    public RoomSwitcher switcher; //a GO holding a delegate called roomSwitched to tell this script about it, change this part to your needing, you somehow have to call onLevelLoad(); in here

    public int framesSinceLevelLoaded =0;
    public int resetPositionAfterFrames = 1; // 1 in my tests
    int framecounter = 0;

    GameObject Stabilizer;


    // Use this for initialization
    void Start ()

    {
    switcher.roomSwitched += onLevelLoad; // as mentioned above
    setupStabilizer ();
    }


    // Update is called once per frame
    void Update ()
    {
    resetCam (framesSinceLevelLoaded++ == resetPositionAfterFrames);
    }


    void onLevelLoad()
    {
    framesSinceLevelLoaded = 0;
    }


    void setupStabilizer()
    {
    Stabilizer = new GameObject ();
    Stabilizer.transform.parent = OVRCaameraRig.transform; //put a game object into the OVR camera, to be draged with it all the time
    Stabilizer.name = "Benjamins Fantastic Oculus VR Level Load Stabilizer";
    }

    void resetCam(bool reset)
    {
    if (reset) {
    Stabilizer.transform.parent = gameObject.transform.parent; //take stabilizer game object from the camera
    gameObject.transform.parent = Stabilizer.transform; //put this game object into the stabilizer
    Stabilizer.transform.localEulerAngles = new Vector3 (0, 0, 0); //reset the local transform of the stabilizer
    Stabilizer.transform.localPosition = new Vector3 (0, 0, 0);
    gameObject.transform.parent = Stabilizer.transform.parent; //put the stabilizer back into the camera
    Stabilizer.transform.parent = OVRCaameraRig.transform;
    } else {
    Stabilizer.transform.parent = gameObject.transform.parent; //take stabilizer game object from the camera
    Stabilizer.transform.localEulerAngles = new Vector3 (0, 0, 0); //reset the local transform of the stabilizer
    Stabilizer.transform.localPosition = new Vector3 (0, 0, 0);
    Stabilizer.transform.parent = OVRCaameraRig.transform; //put the stabilizer back into the camera
    }
    }
    }
    October 25, 2016
    Hey. I'm a beginner. I do in Unity 5 game on the Oculus. Help me - both in the game to make the camera calibration? Thank you.