Search Unity

Activate virtual camera keeping the same rotation

Discussion in 'Cinemachine' started by Mars91, Aug 7, 2018.

  1. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    Hi,
    Everytime I go back to my freelook camera the camera goes back to the old position/rotation when I first switched to my static follow camera.
    Is possible to keep the current position rotation?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    You'll have to give some more details about what you're doing.
    FreeLook sets its axis values according to the input channels specified in their Axis structures.
    Those input values are read while the vcam is live.
    What is the nature of your "static follow camera"? Does it read user input? Maybe you could show some inspector images?
     
  3. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    Hi,
    Here a couple of screenshot for both cameras and a video

    Video:
    https://drive.google.com/open?id=1WwUl4Qs6x5rrM_lDADYEaP699KMSfGSK

    Pic:
    FreeLock: https://drive.google.com/open?id=1LMYJzTymIxAlZxkrxwDNqIcBUDCkdkTo
    Static: https://drive.google.com/open?id=10GIYD9p6BC_f-3-I69b8kf3bsiiqKvg7

    FreeCamInspector.png
    StaticCamInspector.png
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Thanks for that.

    It's still a little unclear to me, but I can hazard a guess (future reference: it is helpful is to enable the "Debug Text" checkbox on the Brain - then I can tell which vcam is active when).

    I'm guessing that the TrackedDolly vcam is active when he's going on the ladder, and that you don't like it when the FreeLook remembers its position from when it was last active, because that makes uncomfortably large blends. You would prefer if the FreeLook picks up where the TrackedDolly leaves off. Is that right?

    If so, try enabling "Inherit Position" in the FreeLook.
     
    Mars91 likes this.
  5. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    572
    That completely solved my problem.

    Another quick questions:
    How can I set the maximum distance from the player/object for a free look camera? Should I edit the various orbits radius to achieve the desired effect?
    Input Axis Name can be any axis I set in the InputManager? I'm asking that because the game will be ported to console too and I will change axis on runtime depending on platform.
    Thank you
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Yes.

    Yes.
     
  7. toomasio

    toomasio

    Joined:
    Nov 19, 2013
    Posts:
    199
    Hello @Gregoryl . How would you keep the same rotation as last camera calling from an extension method. I am having some issues with my camera's position snapping to odd locations when I am using the "Inherit Position" checkbox, especially when my camera switches from first person to third. I only want to inherit rotation values.

    This is the closest I could get, but it snaps back to the POV pipeline hor and ver input. I tried Reset() the hor and ver inputs on the POV component but again it snaps back. Anything else I can try?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5. using MEC;
    6.  
    7. public class CinemachineRotationLockExtension : CinemachineExtension
    8. {
    9.     public enum LockToType { Follow, LookAt, Custom }
    10.     public enum LockAxisType { X, Y, Z }
    11.     [SerializeField] private LockToType lockTo;
    12.     [SerializeField] private int lockAxisMask;
    13.     [SerializeField] private Vector3 rotation;
    14.  
    15.     private Transform tar;
    16.     private float xRot;
    17.     private float yRot;
    18.     private float zRot;
    19.  
    20.     private bool adjustingRotation;
    21.     private Quaternion rotationAdjust;
    22.  
    23.     protected override void PostPipelineStageCallback(CinemachineVirtualCameraBase vcam, CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
    24.     {
    25.         if (stage == CinemachineCore.Stage.Aim)
    26.         {
    27.             var camRot = state.RawOrientation.eulerAngles;
    28.             xRot = camRot.x;
    29.             yRot = camRot.y;
    30.             zRot = camRot.z;
    31.             if (lockTo != LockToType.Custom)
    32.             {
    33.  
    34.                 if (lockTo == LockToType.Follow && vcam.Follow)
    35.                     tar = vcam.Follow;
    36.                 if (lockTo == LockToType.LookAt && vcam.LookAt)
    37.                     tar = vcam.LookAt;
    38.  
    39.                 if (tar)
    40.                 {
    41.                     var rot = tar.rotation.eulerAngles;
    42.  
    43.                     if (lockAxisMask == (lockAxisMask | (1 << (int)LockAxisType.X)))
    44.                         xRot = rot.x;
    45.                     if (lockAxisMask == (lockAxisMask | (1 << (int)LockAxisType.Y)))
    46.                         yRot = rot.y;
    47.                     if (lockAxisMask == (lockAxisMask | (1 << (int)LockAxisType.Z)))
    48.                         zRot = rot.z;
    49.                 }
    50.                
    51.             }
    52.             else
    53.             {
    54.                 xRot = rotation.x;
    55.                 yRot = rotation.y;
    56.                 zRot = rotation.z;
    57.             }
    58.  
    59.             //normal rotation overrides
    60.             if (!adjustingRotation)
    61.                 state.RawOrientation = Quaternion.Euler(new Vector3(xRot, yRot, zRot));
    62.             else
    63.             {
    64.                 //keep last cameras rotation
    65.                 state.RawOrientation = rotationAdjust;
    66.  
    67.                 //adjust POV settings so it stays the same rotation...?
    68.             }
    69.  
    70.         }
    71.     }
    72.  
    73.     public void SetRawOrientationOneFrame(Quaternion _orientation)
    74.     {
    75.         rotationAdjust = _orientation;
    76.         Timing.RunCoroutine(StartCameraAdjust());
    77.     }
    78.  
    79.     IEnumerator<float> StartCameraAdjust()
    80.     {
    81.         adjustingRotation = true;
    82.         yield return Timing.WaitForSeconds(1);//using seconds to debug
    83.         adjustingRotation = false;
    84.     }
    85.  
    86. }
    87.  
     
    andreiagmu likes this.