Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rotation changes when switching between cameras

Discussion in 'Cinemachine' started by pizzaboy13, Apr 25, 2020.

  1. pizzaboy13

    pizzaboy13

    Joined:
    Mar 6, 2019
    Posts:
    18


    Or it might be more accurate to say the rotation stays the same. When I switch perspective, the camera remembers the rotation that I left it in, which is not what I want.

    I am using cinemachine for a first person camera(vcam) and a third person camera (freelook).

    When the camera switches, my goal is to have the camera in the same direction as the previous perspective.

    On the third person camera

    It uses the cinemachine freelook's default camera control.

    On the first person camera

    Code (CSharp):
    1. // Handle camera rotation
    2. if (invertX)
    3. {
    4.     xRot -= Input.GetAxis("Mouse Y");
    5. }
    6. else
    7. {
    8.     xRot += Input.GetAxis("Mouse Y");
    9. }
    10.  
    11. if (invertY)
    12. {
    13.     yRot -= Input.GetAxis("Mouse X");
    14. }
    15. else
    16. {
    17.     yRot += Input.GetAxis("Mouse X");
    18. }
    19.  
    20. xRot = Mathf.Clamp(xRot, verticalUpperLimit, verticalLowerLimit);
    21. firstPersonCamera.transform.localRotation = Quaternion.Euler(xRot, yRot, 0f);
    22.  
    23. // Offset y rotation when looking down
    24. var transposer = firstPersonCamera.GetCinemachineComponent<CinemachineTransposer>();
    25. float percentToVerticalMin = Mathf.InverseLerp(0, verticalLowerLimit, xRot);
    26. transposer.m_FollowOffset = Vector3.Lerp(defaultCameraPosition, defaultCameraPosition + cameraDownOffset, percentToVerticalMin);
    27.            
    On the player character

    in third person, the character rotates towards the movement direction, in first person, the character rotates towards the camera forward direction:

    Code (CSharp):
    1. // Update position
    2. Vector3 rawTargetSpeed = mainCamera.transform.forward * Input.GetAxis("Vertical") + mainCamera.transform.right * Input.GetAxis("Horizontal");
    3. rawTargetSpeed.y = 0f;
    4. Vector3 targetSpeed = rawTargetSpeed.normalized;
    5.  
    6. if (Input.GetButton("Run"))
    7. {
    8.     // Speed change
    9.     targetSpeed *= runSpeedMultiplier;
    10. }
    11.  
    12. characterController.SimpleMove(targetSpeed * speed);
    13.  
    14. // Update rotation
    15.  
    16. switch (movementType)
    17. {
    18.     case MovementType.Default:
    19.         break;
    20.     case MovementType.ThirdPerson:
    21.         targetForward = targetSpeed;
    22.         maxDeltaRadians = turnSpeed * Time.deltaTime;
    23.         break;
    24.     case MovementType.FirstPerson:
    25.         targetForward = mainCamera.transform.forward;
    26.         maxDeltaRadians = turnSpeed * Time.deltaTime * 10;
    27.         break;
    28.     default:
    29.         break;
    30. }
    31.  
    32. Vector3 targetRotation = Vector3.RotateTowards(transform.forward, targetForward, maxDeltaRadians, 0f);
    33. targetRotation.y = 0f;
    34. transform.rotation = Quaternion.LookRotation(targetRotation);
    35.  
    Here is a solution I tried, basically when switching from first person to third person:
    • Turn off cinemachine (tried with on aswell)
    • Reset rotation
    • Parent the first person camera to the played
    • When switching back to first person, do the reverse
    No luck. Any ideas what could be causing this?
     
    Last edited: Apr 25, 2020
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Have you tried this option on your vcams?

    upload_2020-4-25_8-33-49.png
     
  3. pizzaboy13

    pizzaboy13

    Joined:
    Mar 6, 2019
    Posts:
    18
    I just did, made no difference unforunately
     
  4. rubiez64

    rubiez64

    Joined:
    Jul 25, 2016
    Posts:
    26
    same here, I'm trying to blend from virtual camera with tracked dolly, to a virtual camera with Transposer.
    Inherit position doesn't work for me.
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    @PizzaBoy It's a little hard to tell what's going on, since you have custom code and it's unclear how it interacts with CM. Can you put together a lightweight test project, with your custom code, that shows this problem?
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    @rubiez64 Can you describe what you're trying to achieve, and what result you're getting?