Pause Menu: Only 1 Camera Stops while both follow same code!
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:
And in MouseLook I created a gameRunning float and would change it from my PauseMenuCode.
A Part of MouseLook looked like this:
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
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:
Above, I added the public float gameRunning that is changed whenever the escape button is pressed from my PauseMenu.
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
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:
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 :D
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 :D
