cancel
Showing results for 
Search instead for 
Did you mean: 

Creating a Pause Menu For Oculus Camera ERROR!

RobotReactor
Honored Guest
So I have a problem that I don't know if it is just me or a bug with the Oculus Rift code its self.

I have created successful codes in the past before I tried coding for the Oculus Rift and it was very simple.
The code consisted of this:

using UnityEngine;
using System.Collections;

public class PauseMenuCode : MonoBehaviour
{
// public GameObject mL;
int Paused = 0;
int notPaused = 1;
int running;
MouseLook mouselook;
// Use this for initialization
void Start ()
{
Screen.lockCursor = true;
Screen.showCursor = true;
Time.timeScale = Paused;
mouselook = GetComponent<MouseLook>();

}

void OnGUI() {
if (Time.timeScale == Paused && running != 1) {
if (GUI.Button(new Rect(Screen.width/2,Screen.height/2,100,50),"Start Game")) {
Time.timeScale = notPaused;
running = 1;
mouselook.gameRunning = 1;
}
}
else if (Time.timeScale == Paused && running == 1) {
if (GUI.Button(new Rect(Screen.width/2,Screen.height/2,100,50),"Resume Game")) {
Time.timeScale = notPaused;
mouselook.gameRunning = 1;
}
}
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape) && Time.timeScale == notPaused && running != 0) {
Screen.lockCursor = false;
Screen.showCursor = true;
Time.timeScale = Paused;
mouselook.gameRunning = 0;
}
else if ( Input.GetKeyDown(KeyCode.Escape) && Time.timeScale == Paused && running != 0) {
Screen.lockCursor = true;
Screen.showCursor = false;
Time.timeScale = notPaused;
mouselook.gameRunning = 1;
}
if (Time.timeScale != Paused) {
Screen.lockCursor = true;
}
}
}


And in MouseLook I created a gameRunning float and would change it from my PauseMenuCode.
A Part of MouseLook looked like this:

public float gameRunning = 0F;

void Update () {

if (gameRunning != 0) {
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}


That worked very well and I was very pleased!
I integrated that into the Oculus Rift code in the project I made and it had a couple problems, and one I STILL CAN'T FIX!

It would "Pause" the game with Time.timeScale = Paused; (Or Time.timeScale = 0;) But The cameras would still track the Oculus movement (It doesnt track the mouse movement when 'paused')... so for a few hours (Of debugging and reading and understanding the code) I finally put
 if (gameRunning == notPaused) { ~Do the code where the cameras would change(Get) the orientation of the Oculus Rift~}
In the right place.

Now.. My main problem with this is that yes, it stops following the camera. But only the left camera. When I look at the inspector for the CameraLeft OVRCamera (Script), whenever I hit escape, Game Running changes from either 0 to 1(Paused to not Paused) or 1 to 0 (not Paused to Paused). Great! But... the right camera never changes whenever I hit escape. Therefore, the camera continues to follow my every movement, even hitting my head on the desk. It is one of the most frustrating errors I have ever encountered because of how it seems like it should work, but it teases me and makes me dizzy. Here is the code in OVRCamera:

using UnityEngine;
using System.Runtime.InteropServices;

[RequireComponent(typeof(Camera))]

//-------------------------------------------------------------------------------------
// ***** OVRCamera
//
// OVRCamera is used to render into a Unity Camera class.
// This component handles reading the Rift tracker and positioning the camera position
// and rotation. It also is responsible for properly rendering the final output, which
// also the final lens correction pass.
//

public class OVRCamera : OVRComponent
{
#region Member Variables
// PRIVATE MEMBERS
// If CameraTextureScale is not 1.0f, we will render to this texture
private RenderTexture CameraTexture = null;
// Default material, just blit texture over to final buffer
private Material BlitMaterial = null;
// Color only material, used for drawing quads on-screen
private Material ColorOnlyMaterial = null;
private Color QuadColor = Color.red;
// Scaled size of final render buffer
// A value of 1 will not create a render buffer but will render directly to final
// backbuffer
private float CameraTextureScale = 1.0f;

// We will search for camera controller and set it here for access to its members
private OVRCameraController CameraController = null;

public float gameRunning = 1F;

Above, I added the public float gameRunning that is changed whenever the escape button is pressed from my PauseMenu.

void SetCameraOrientation()
{

Quaternion q = Quaternion.identity;
Vector3 dir = Vector3.forward;
//if(gameRunning == 1) {
//Debug.Log (gameRunning);
// Main camera has a depth of 0, so it will be rendered first
if(gameObject.camera.depth == 0.0f)
{
// If desired, update parent transform y rotation here
// This is useful if we want to track the current location of
// of the head.
// TODO: Future support for x and z, and possibly change to a quaternion
if(CameraController.TrackerRotatesY == true)
{
Vector3 a = gameObject.camera.transform.rotation.eulerAngles;
a.x = 0;
a.z = 0;
gameObject.transform.parent.transform.eulerAngles = a;
}

// Read shared data from CameraController
if(CameraController != null)
{
// Read sensor here (prediction on or off)
if(CameraController.PredictionOn == false)
OVRDevice.GetOrientation(0, ref CameraOrientation);
else
OVRDevice.GetPredictedOrientation(0, ref CameraOrientation);
}

// This needs to go as close to reading Rift orientation inputs
OVRDevice.ProcessLatencyInputs();

}
// Calculate the rotation Y offset that is getting updated externally
// (i.e. like a controller rotation)
float yRotation = 0.0f;
CameraController.GetYRotation(ref yRotation);
q = Quaternion.Euler(0.0f, yRotation, 0.0f);
dir = q * Vector3.forward;
q.SetLookRotation(dir, Vector3.up);

// Multiply the camera controllers offset orientation (allow follow of orientation offset)
Quaternion orientationOffset = Quaternion.identity;
CameraController.GetOrientationOffset(ref orientationOffset);
q = orientationOffset * q;


// Multiply in the current HeadQuat (q is now the latest best rotation)
if(CameraController != null)
q = q * CameraOrientation;

// * * *
// Update camera rotation
//Here I add the code for if the game is running, find the orientation and set it
if(gameRunning != 0) {
gameObject.camera.transform.rotation = q;
}
// * * *
// Update camera position (first add Offset to parent transform)
gameObject.camera.transform.position =
gameObject.camera.transform.parent.transform.position + NeckPosition;

// Adjust neck by taking eye position and transforming through q
gameObject.camera.transform.position += q * EyePosition;
}


Now, the gameRunning if statement I coded should work for both cameras because they both follow the same script. And i even completely coded out the

if(gameRunning != 0) {
gameObject.camera.transform.rotation = q;
}

And both Cameras would not track anything! Its exactly how I planned my added code to work..
Here is the Pause Menu code I have:


using UnityEngine;
using System.Collections;

public class PauseMenuCode : MonoBehaviour {

private int running = 1;
OVRCamera camera;

public void Start(){
running = 1;
Time.timeScale = 1;
AudioListener.volume = 1;
Screen.showCursor = false;
Screen.lockCursor = true;
camera = GetComponentInChildren<OVRCamera>();

}

public void Update(){

if(Input.GetKeyDown("escape")){

//check if game is already paused, then unpause it
if(running == 0){
running = 1;
Time.timeScale = 1;
Screen.showCursor = false;
camera.gameRunning = 1;
}

//check if game isn't paused, then pause it
else if(running == 1){
running = 0;
Time.timeScale = 0;
Screen.showCursor = true;
camera.gameRunning = 0;
}
}
}

public void OnGUI(){

if(running != 1){

//Make a background box
GUI.Box(new Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "Pause Menu");

//Quit
if (GUI.Button(new Rect(Screen.width /2 - 100,Screen.height /2 + 50,250,50), "Quit Game")){
Application.Quit();
}
}
}
}


Why is it that the right camera is not following the SAME CODE the left uses? Please help 😞

Help would be very appreciated and I am willing to answer any questions to help with solving this problem. :cry:

Thank you very much in advance 😄
1 REPLY 1

Species5618
Honored Guest
HI there,

I implemented your code, and it is a strange problem. I do not know what is causing it but I think I found a simple fix for you.

Somehow the script cant access the right camera variable , might have something to do with the fact that the script is shared for the two camera's i think (but I did not research the OVRCamera class in dept yet).

But, wat I did is created 2 new classes that extend on the original OVRCamera class, so we can access them independently, and bypass the issue.

So just create these two:


// this C# code script must replace the orginal Right OVRCamera script
using UnityEngine;
using System.Collections;

public class ROVRCamera : OVRCamera
{

}



// this C# code script must replace the orginal Left OVRCamera script
using UnityEngine;
using System.Collections;

public class LOVRCamera : OVRCamera
{

}


Now you must change the classes that are called in the pausemenu script to call these new extended ones, look for
the part that have /// EDIT: ....


using UnityEngine;
using System.Collections;

public class PauseMenuCode : MonoBehaviour
{
private int running = 1;

/// EDITED: Remove orginale single reference
//OVRCamera camera;

//// EDITED: Replace it with 2 seperate ones
LOVRCamera cameraL;
ROVRCamera cameraR;

public void Start()
{
running = 1;
Time.timeScale = 1;
AudioListener.volume = 1;
Screen.showCursor = false;
Screen.lockCursor = true;

//// EDITED: Remove orginale single reference
// camera = GetComponentInChildren<OVRCamera>();

//// EDITED: Replace it with 2 seperate ones
cameraL = GetComponentInChildren<LOVRCamera>();
cameraR = GetComponentInChildren<ROVRCamera>();
}

public void Update()
{

if(Input.GetKeyDown("escape"))
{

//check if game is already paused, then unpause it
if(running == 0)
{
running = 1;
Time.timeScale = 1;
Screen.showCursor = false;

//// EDITED: Remove orginale single reference
// camera.gameRunning = 1;

//// EDITED: Replace it with 2 seperate ones
cameraL.gameRunning = 1;
cameraR.gameRunning = 1;
}

//check if game isn't paused, then pause it
else if(running == 1)
{
running = 0;
Time.timeScale = 0;
Screen.showCursor = true;

//// EDITED: Remove orginale single reference
// camera.gameRunning = 0;

//// EDITED: Replace it with 2 seperate ones
cameraL.gameRunning = 0;
cameraR.gameRunning = 0;
}
}
}

public void OnGUI()
{
if(running != 1)
{
//Make a background box
GUI.Box(new Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "Pause Menu");

//Quit
if (GUI.Button(new Rect(Screen.width /2 - 100,Screen.height /2 + 50,250,50), "Quit Game"))
{
Application.Quit();
}
}
}
}


I have tested it and in unity4 it seems to work. The left and right camera now show "1" or "0" together depending of the last state offcourse.

let me know if it helped you

cheers
Still need help?

Did this answer your question? If it didn’t, use our search to find other topics or create your own and other members of the community will help out.

If you need an agent to help with your Meta device, please contact our store support team here.

Having trouble with a Facebook or Instagram account? The best place to go for help with those accounts is the Facebook Help Center or the Instagram Help Center. This community can't help with those accounts.

Check out some popular posts here:

Getting Help from the Meta Quest Community

Tips and Tricks: Charging your Meta Quest Headset

Tips and Tricks: Help with Pairing your Meta Quest

Trouble With Facebook/Instagram Accounts?