Search Unity

Rotate camera head with joystick regardless of room space position

Discussion in 'AR/VR (XR) Discussion' started by PatrickReynolds, Aug 10, 2017.

  1. PatrickReynolds

    PatrickReynolds

    Joined:
    Nov 19, 2014
    Posts:
    17
    I am trying to set up a simple camera rotation system, and have run into a problem getting the Oculus camera rig to rotate properly. My camera is nested to a parent object like this:
    • VRCameraOBJ
      • OVRCameraRig
        • TrackingSpace
          • CenterEyeAnchor
      • LocalAvatar
      • CustomInputScr
    Whenever I try to rotate the camera using this script: (smooth rotation)

    Code (CSharp):
    1. VRCameraObj.transform.Rotate(Vector3.up, OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).x * rotationSpeed);
    or this script: (15 degree rotation)

    Code (CSharp):
    1.  
    2. if (OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).x < -.9 && currentRotationDelay <= 0.0f)
    3.             {
    4.                 //Rotate the camera by -15 degrees
    5.                 Vector3 newRot = new Vector3(VRCameraObj.transform.rotation.eulerAngles.x, VRCameraObj.transform.rotation.eulerAngles.y -15, VRCameraObj.transform.rotation.eulerAngles.z);
    6.                 VRCameraObj.transform.rotation = Quaternion.Euler(newRot);
    7.  
    8.                 //Set the tracker for rotation delay
    9.                 currentRotationDelay = rotationDelay;
    10.             }
    11.             if (OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).x > .9 && currentRotationDelay <= 0.0f)
    12.             {
    13.                 //Rotate the camera by -15 degrees
    14.                 Vector3 newRot = new Vector3(VRCameraObjtransform.rotation.eulerAngles.x, VRCameraObj.transform.rotation.eulerAngles.y + 15, VRCameraObj.transform.rotation.eulerAngles.z);
    15.                 VRCameraObj.transform.rotation = Quaternion.Euler(newRot);
    16.  
    17.                 //Set the tracker for rotation delay
    18.                 currentRotationDelay = rotationDelay;
    19.             }
    20.  
    the entire camera system rotates about the center of the VRCameraObj, rather than the center of the Tracking space, which does not necessarily align with the VRCameraObj's position because of local movement in the oculus' VR Room Space. (i.e., the user takes a step to the right, and the CenterEyeAnchor's position becomes 0,1,0, rather than 0,0,0)

    This image illustrates what I'm talking about:




    The center circle is the location of the VRCameraOBJ, while the outer circles are where the camera is winding up when I try to rotate them using the code above.

    I understand why this is happening, because I'm rotating the parent object rather than the object itself (VRCameraObj instead of CenterEyeAnchor). The issue is that there is no way I know of to directly manipulate the rotation of CenterEyeAnchor, and rotating anything else (TrackingSpace or OVRCameraRig) results in the same offset rotation. This is the code I used to try rotating other Game Objects: (for smooth rotation)

    Code (CSharp):
    1.                 OVRCameraRig.transform.Rotate(Vector3.up, OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).x * rotationSpeed);
    2.                 OVRCameraRig.transform.RotateAround(OVRCameraRig.transform.position, Vector3.up, OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).x * rotationSpeed);
    3.                 centerEyeAnchor.transform.Rotate(Vector3.up, OVRInput.Get(OVRInput.RawAxis2D.RThumbstick).x * rotationSpeed, Space.Self);
    Is there a way to rotate the CenterEyeAnchor its own origin point so that I can rotate it while still allowing the user to move around within the bounds of Unity's room space?

    I've tried one other fix, moving the VRCameraObj to the CenterEyeAnchor's x and z position, then resetting CenterEyeAnchor's local position to 0,0,0, but Unity doesn't allow you to directly manipulate the local positions of any of the OVRCamera's transform data.

    Code (CSharp):
    1. if(centerEyeAnchor.transform.localPosition.x != 0 || centerEyeAnchor.transform.localPosition.z != 0)
    2.         {
    3.             float xDif = centerEyeAnchor.transform.localPosition.x;
    4.             float zDif = centerEyeAnchor.transform.localPosition.z;
    5.  
    6.             //Adjsut the location of gameobject
    7.             VRCameraObj.transform.position = new Vector3(VRCameraObj.transform.position.x + xDif,
    8.                                                         VRCameraObj.transform.position.y,
    9.                                                         VRCameraObj.transform.position.z + zDif);
    10.  
    11.             //Reset the local location of the center eye anchor
    12.             centerEyeAnchor.transform.localPosition = new Vector3(0.0f, centerEyeAnchor.transform.localPosition.y, 0.0f);
    13.         }
    Any help with this would be greatly appreciated. It's nauseating to try to essentially rotate your head, but also wind up moving along the ground at the same time.

    Thanks you!
     
  2. lumpy_dev

    lumpy_dev

    Joined:
    Mar 7, 2018
    Posts:
    1
    Did you ever find a solution to this?