Search Unity

Question Smoothing Cinemachine 3rd person camera

Discussion in 'Cinemachine' started by Jman2150, Aug 11, 2020.

  1. Jman2150

    Jman2150

    Joined:
    Aug 23, 2017
    Posts:
    10
    I'm trying to create a simple third person follow camera using Cinemachine, and while I'm very happy with how simple it was to setup, I'm having some trouble getting it to move smoothly around the player. I'm using a gamepad for input and as I move the joystick in any direction and with any intensity, the camera will move in a very jerky way (link to video below). I know it's not a frame rate issue, so I assume that there is either a cinemachine setting I'm not aware of that can smooth this out, or there is something I could do in the camera control script to make it move more smoothly. Just looking for some feedback from folks who have worked with this before.

    https://www.dropbox.com/s/zzk0crxs93c7klv/2020-08-11 18-18-45.mp4?dl=0

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5. using UnityEngine.InputSystem;
    6. using System.Linq.Expressions;
    7.  
    8. [RequireComponent(typeof(CinemachineFreeLook))]
    9. public class FreeLookAddOn : MonoBehaviour
    10. {
    11.     [Range(0f, 100f)] public float LookSpeed = 50f;
    12.     public bool InvertY = false;
    13.     private CinemachineFreeLook _freeLookComponent;
    14.     PlayerControls controls;
    15.     Vector2 rotate;
    16.  
    17.     public void Awake()
    18.     {
    19.         controls = new PlayerControls();
    20.         _freeLookComponent = GetComponent<CinemachineFreeLook>();
    21.  
    22.         controls.Gameplay.Rotate.performed += ctx1 => rotate = ctx1.ReadValue<Vector2>();
    23.         controls.Gameplay.Rotate.canceled += ctx1 => rotate = Vector2.zero;
    24.     }
    25.     private void Update()
    26.     {
    27.         Vector2 lookMovement = new Vector2(rotate.x,rotate.y).normalized * Time.deltaTime;
    28.         lookMovement.y = InvertY ? -lookMovement.y : lookMovement.y;
    29.  
    30.         lookMovement.x = lookMovement.x * 180f;
    31.  
    32.         _freeLookComponent.m_XAxis.Value += lookMovement.x * LookSpeed * Time.deltaTime;
    33.         _freeLookComponent.m_YAxis.Value += lookMovement.y * LookSpeed * Time.deltaTime;
    34.     }
    35.  
    36.     private void OnEnable()
    37.     {
    38.         controls.Gameplay.Enable();
    39.     }
    40.  
    41.     private void OnDisable()
    42.     {
    43.         controls.Gameplay.Disable();
    44.     }
    45.  
    46. }
     
    mysticvalleycsa likes this.
  2. Jman2150

    Jman2150

    Joined:
    Aug 23, 2017
    Posts:
    10
    As would appear to be a habit of mine at this point, some hours after posting I have discovered my own error. Without realizing it I was double dipping on Time.deltaTime. I multiplied both the vector that defined the direction I wanted to move AND the components of the cinemachine that were being moved by Time.deltaTime. I'm guessing this means that, rather than making the movement frame rate independent, as intended, it ended up doubling the amount of moment I did per frame and that led to the jitter. Or something to that effect anyway.
     
    mysticvalleycsa and Gregoryl like this.