Search Unity

Rotation of Camera

Discussion in 'Xiaomi' started by moldywarpe, Oct 22, 2018.

  1. moldywarpe

    moldywarpe

    Joined:
    Jan 18, 2017
    Posts:
    31
    Hi,
    I am having trouble with rotating a view from a camera.

    The problem is that I have a scene with multiple cameras and want to switch from one camera to another that is rotated by 90 degrees, however switching from one to the other rotates the view for a split second before the MiCamera script resets the view back to the HMD position of the initial camera.

    The setup is a single Camera controller gameobject with the VRManager script attached. This has two MiCamera prefabs as children with only one active at a time and one rotated 90 degrees.

    When "playing" within the editor the MiEmulation class does an update to set the head rotation in line with the position of the mouse, effectively resetting the camera rotation when switching from one camera to the other.
    When using the headset the the MiHMD class does the same thing but bases the rotation on the position of the HMD obtained from the HMD sensor data.


    I have previously used this technique with both Oculus and Pico without any issues... so is this a difference in architecture that is preventing me from changing camera views or am I just going about this the wrong way????

    Cheers.
     
  2. moldywarpe

    moldywarpe

    Joined:
    Jan 18, 2017
    Posts:
    31
    Ok, my solution... for anybody interested...

    At the time I want to switch cameras I had to update the MiTransform hmdLeftEye within MiCamera.UpdateTransforms() and set it to the Quaternion value I wanted the direction my camera to be pointing.

    e.g. example code for VR headset
    Code (CSharp):
    1. private void UpdateTransforms()
    2. {
    3. #if UNITY_ANDROID && !UNITY_EDITOR
    4.     MiTransform hmdLeftEye = VrManager.Instance.Hmd.GetEyeTransform(Eyes.Left);
    5.     MiTransform hmdRightEye = VrManager.Instance.Hmd.GetEyeTransform(Eyes.Right);
    6.     // Debug.Log(string.Format("hmdLeftEye rotation is {0}", hmdLeftEye.Rotation));
    7.  
    8.     // Mod to update current MiTransform on change of camera
    9.     if (Control.activeCamera.name != Control.previousCameraName)
    10.     {
    11.         // Rotate for appropriate camera
    12.         if (Control.activeCamera.name.Equals("Camera1"))
    13.        transform.rotation = new Quaternion(0.0f, 0.0f, 0.0f, 1.0f); // 0 degrees
    14.         else if (Control.activeCamera.name.Equals("Camera2"))
    15.        transform.rotation = new Quaternion(0.0f, 0.7f, 0.0f, 0.7f); // 90 degrees
    16.         else if (Control.activeCamera.name.Equals("Camera3"))
    17.        transform.rotation = new Quaternion(0.0f, 0.7f, 0.0f, -0.7f); // -90 deg    
    18.         else if (Control.activeCamera.name.Equals("Camera4"))
    19.        transform.rotation = new Quaternion(0.0f, -1.0f, 0.0f, 0.0f); // 180 deg
    20.         // Update the left eye head rotation quaternion for the required camera
    21.         hmdLeftEye.Rotation = transform.rotation;
    22.         Control.previousCameraName = Control.activeCamera.name;
    23.     }
    24.     // End modification
    25.  
    26.     if ( !LockRotation )
    27.     {
    28.        this.transform.localRotation = hmdLeftEye.Rotation;
    29.     }
    30.    
    31.      this.CenterEyeTransform.localPosition = 0.5f * (hmdLeftEye.Position + hmdRightEye.Position);
    32.      this.LeftEyeTransform.localPosition = hmdLeftEye.Position;
    33.      this.RightEyeTransform.localPosition = hmdRightEye.Position;
    34. }
    35.  
    Same sort of thing whilst running in editor, update the MiEmulation headEulerAngles within MiCamera.UpdateTransforms() and set them to what I want at the time of the camera change.

    Cheers