Search Unity

Cinemachine Virtual Camera - Follow Offset Value Change Jitter

Discussion in 'Cinemachine' started by rtilton1, Dec 17, 2019.

  1. rtilton1

    rtilton1

    Joined:
    Jan 4, 2017
    Posts:
    62
    I'm using a cinemachine virtual camera. Follow and LookAt are both set to my player transform. The body of the camera is set to Transposer, binding mode: worldspace, with 0 dampening on x, y, and z.

    In my code I'm changing the follow offset when when the camera can't see the player at the normal follow offset:

    Code (CSharp):
    1.  
    2. CinemachineVirtualCamera cineCam;
    3. CinemachineTransposer cineTransposer;
    4. Transform player;
    5.  
    6. void Start()
    7. {
    8.     cineCam = this.GetComponent<CinemachineVirtualCamera>();
    9.     cineTransposer = cineCam.GetCinemachineComponent<CinemachineTransposer>();
    10.     player = GameObject.FindGameObjectWithTag(Tags.player).transform;
    11. }
    12.  
    13. void Update()
    14. {
    15.       //funciton to raycast toward player from normal camera offset pos and see if it's viewable
    16.       bool isNormalViewingPos = ViewingPosCheck(player.position + new Vector3(-15f, 40f, -15f));
    17.  
    18.       //if can't see player at normal viewing angle - move to secondary viewing angle
    19.       if (!isNormalViewingPos)
    20.       {
    21.            cineTransposer.m_FollowOffset = new Vector3(-5f, 40f, -5f);
    22.       }
    23.       else
    24.       {
    25.             cineTransposer.m_FollowOffset = new Vector3(-15f, 40f, -15f);
    26.       }
    27. }
    28.  
    The issue is that the transition to the new follow offset looks really jumpy and not smooth. It's looks as if it tries to transition from a different position to the new position rather than smoothly going from where it is to where the new values are.
     
  2. rtilton1

    rtilton1

    Joined:
    Jan 4, 2017
    Posts:
    62
    Just realized the jumpy issue is caused from the camera's rotation being slow to rotate while the offset snaps into place immediately. I then tried lerping the offset position to give the camera time to rotate and move smoothly. Now the camera is all stuttery when moving the offset in a Vector3.Lerp...

    Code (CSharp):
    1.  
    2. if (!isNormalViewingPos)
    3.         {
    4.             cineTransposer.m_FollowOffset = Vector3.Lerp(cineTransposer.m_FollowOffset, new Vector3(-5f, 40f, -5f), .02f);
    5.         }
    6.         else
    7.         {
    8.             cineTransposer.m_FollowOffset = Vector3.Lerp(cineTransposer.m_FollowOffset, new Vector3(-15f, 40f, -15f), .02f);
    9. }
    10.  

    How would I animate this smoothly?
     
  3. rtilton1

    rtilton1

    Joined:
    Jan 4, 2017
    Posts:
    62
    Changed my virtual camera's "Anim" to Hard Look At in the inspector and the jitter went away... Not sure if that's the right approach, but at least it's working as expected now.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    There are a couple of approaches that might be better:
    1. Use 2 vcams, each with different offsets (and potentially other differences for added finesse), and activate the one you want, when you want it. Let the CM brain do a nice smooth transition for you.
    2. Have an invisible object following the player at the desired offset, and use that as a follow target. When the offset suddenly changes, the Transposer damping on the vcam will smooth it out.
     
    nickseegobin and mrtclgst like this.
  5. rtilton1

    rtilton1

    Joined:
    Jan 4, 2017
    Posts:
    62
    Very interesting. Thanks for the response Greg! I appreciate your help!