Search Unity

Cinemachine camera jitter

Discussion in 'Cinemachine' started by Salja, Oct 2, 2020.

  1. Salja

    Salja

    Joined:
    Mar 23, 2017
    Posts:
    353
    Hi , I've made a character controller linked the FreeLook cinemachine to follow and look at my player.
    My problem is that my camera is starting to jittering.
    I'm not sure if there is something with my code in Update and FixedUpdate, I'm also not exactly sure what exactly needs to be in FixedUpdate.
    It would be nice if someone could help







    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using StriXInteractive.Input;
    5.  
    6. namespace StriXInteractive.PlayerController {
    7.     public class PlayerController_v2 : MonoBehaviour {
    8.         #region Vabrialen
    9.  
    10.         public CharacterController m_CharacterController;
    11.         public InputManager m_Input;
    12.  
    13.         [Tooltip("Set Players Gravity")]
    14.         public float m_Gravity = 20f;
    15.         public float m_StepDown = 0.3f;
    16.         public float m_DirectionSmoothDamp = 0.25f;
    17.         public float m_SpeedSmoothDamp = 0.15f;
    18.         public float m_TurnSmoothTime = 0.1f;
    19.  
    20.         public KeyCode m_SprintKey = KeyCode.LeftShift;
    21.  
    22.         private float m_Speed;
    23.         private float m_SpeedFactor = 0.5f;
    24.         private float m_FallTime;
    25.  
    26.         private float m_TurnSmoothVelocity;
    27.  
    28.         private float m_Horizontal;
    29.         private float m_Vertical;
    30.  
    31.         private Animator m_Animator;
    32.         private Camera m_Camera;
    33.  
    34.         private Vector3 m_RootMotion;
    35.         private Vector3 m_Velocity;
    36.         private Vector3 m_Direction;
    37.         private Vector3 m_MoveDirection;
    38.  
    39.         private bool m_IsGrounded;
    40.         private bool m_IsFalling;
    41.         private bool m_IsSprinting;
    42.  
    43.         public float m_Stamina;
    44.         public float m_MaxStamina = 10f;
    45.  
    46.         public float m_Health;
    47.         public float m_MaxHealth = 100f;
    48.  
    49.         #endregion
    50.  
    51.         #region Start
    52.  
    53.         private void Start() {
    54.             m_Animator = this.GetComponent<Animator>();
    55.             m_Camera = Camera.main;
    56.         }
    57.  
    58.         #endregion
    59.  
    60.         #region Update
    61.  
    62.         private void Update() {
    63.             m_Horizontal = m_Input.GetHorizontalRaw();
    64.             m_Vertical = m_Input.GetVerticalRaw();
    65.  
    66.             m_Direction = new Vector3(m_Horizontal, 0, m_Vertical).normalized;
    67.  
    68.             m_Speed = m_Direction.magnitude * m_SpeedFactor;
    69.  
    70.             if (m_Speed >= 0.2f) {
    71.                 float m_TargetAngle = Mathf.Atan2(m_Direction.x, m_Direction.z) * Mathf.Rad2Deg + m_Camera.transform.eulerAngles.y;
    72.                 float m_Angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, m_TargetAngle, ref m_TurnSmoothVelocity, m_TurnSmoothTime);
    73.                 transform.rotation = Quaternion.Euler(0f, m_Angle, 0f);
    74.  
    75.                 m_MoveDirection = Quaternion.Euler(0f, m_TargetAngle, 0f) * Vector3.forward;
    76.                 m_MoveDirection = m_MoveDirection.normalized;
    77.             }
    78.  
    79.             m_Animator.SetBool("IsGrounded", m_IsGrounded);
    80.             m_Animator.SetFloat("FallTime", m_FallTime);
    81.  
    82.             m_Animator.SetFloat("Speed", m_Speed, m_SpeedSmoothDamp, Time.deltaTime);
    83.             m_Animator.SetFloat("Horizontal", m_MoveDirection.x, m_DirectionSmoothDamp, Time.deltaTime);
    84.             m_Animator.SetFloat("Vertical", m_MoveDirection.z, m_DirectionSmoothDamp, Time.deltaTime);
    85.  
    86.             Sprint();
    87.             CalculateFalltime();
    88.             CalculateStamina();
    89.         }
    90.  
    91.         #endregion
    92.  
    93.         #region FixedUpdate
    94.  
    95.         private void FixedUpdate() {
    96.             // Set Gravity to the Player if he is Falling
    97.             if (m_IsFalling) {
    98.                 m_Velocity.y -= m_Gravity * Time.fixedDeltaTime;
    99.                 m_CharacterController.Move(m_Velocity * Time.fixedDeltaTime);
    100.                 m_IsFalling = !m_IsGrounded;
    101.             }
    102.             else {
    103.                 m_CharacterController.Move(m_RootMotion + Vector3.down * m_StepDown);
    104.                 m_RootMotion = Vector3.zero;
    105.  
    106.                 if (!m_IsGrounded) {
    107.                     m_IsFalling = true;
    108.                     m_Velocity = m_Animator.velocity;
    109.                     m_Velocity.y = 0;
    110.                 }
    111.             }
    112.  
    113.             m_IsGrounded = m_CharacterController.isGrounded;
    114.         }
    115.  
    116.         #endregion
    117.  
    118.         #region OnAnimatorMove
    119.  
    120.         private void OnAnimatorMove() {
    121.             m_RootMotion += m_Animator.deltaPosition;
    122.         }
    123.  
    124.         #endregion
    125.  
    126.         #region Functions
    127.  
    128.         public float CalculateFalltime() {
    129.             // Calculate Player Falltime
    130.             return m_FallTime = !m_IsGrounded ? m_FallTime += Time.deltaTime * 1.0f : 0.0f;
    131.         }
    132.  
    133.         public void Sprint() {
    134.             // Check if the Sprint Input is pessed
    135.             bool m_InputPressed = m_Input.GetKey(m_SprintKey);
    136.             // return true if the Input is pressed and the player speed is greather then 0.2
    137.             m_IsSprinting = m_InputPressed && m_Speed > 0.2f ? true : false;
    138.  
    139.             // Set the Speedfactor if the Player press the Sprinting Input
    140.             // Check if the Player has Stamina for Sprinting
    141.             m_SpeedFactor = m_IsSprinting && m_Stamina > 0.0f ? m_SpeedFactor = 1.0f : m_SpeedFactor = 0.5f;
    142.  
    143.             m_Speed = Mathf.Lerp(m_Speed, m_SpeedFactor, Time.deltaTime);
    144.         }
    145.  
    146.         public void CalculateStamina() {
    147.             // Calculate Stamina for Sprinting
    148.             if (m_IsSprinting) {
    149.                 if (m_Stamina > 0) {
    150.                     m_Stamina = m_Stamina - 2.0f * Time.deltaTime;
    151.                 }
    152.                 else {
    153.                     m_Stamina = 0;
    154.                 }
    155.             }
    156.             else {
    157.                 if (m_Stamina < m_MaxStamina) {
    158.                     m_Stamina = m_Stamina + 0.5f * Time.deltaTime;
    159.                 }
    160.                 else {
    161.                     m_Stamina = m_MaxStamina;
    162.                 }
    163.             }
    164.         }
    165.  
    166.         #endregion
    167.     }
    168. }
    Screenshot_1.png Screenshot_2.png Screenshot_3.png
     
    Last edited: Oct 2, 2020
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Can you send me the project?
     
  3. Salja

    Salja

    Joined:
    Mar 23, 2017
    Posts:
    353
    I send you the Project thank you
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Got the project, thanks.

    The solution is to move all the code from FixedUpdate() into Update(). I just combined them, being careful to replace
    Time.fixedDeltaTime
    with
    Time.deltaTime
    . Like this:
    Code (CSharp):
    1.         private void Update() {
    2.             m_Horizontal = m_Input.GetHorizontalRaw();
    3.             m_Vertical = m_Input.GetVerticalRaw();
    4.  
    5.             m_Direction = new Vector3(m_Horizontal, 0, m_Vertical).normalized;
    6.  
    7.             m_Speed = m_Direction.magnitude * m_SpeedFactor;
    8.  
    9.             if (m_Speed >= 0.2f) {
    10.                 float m_TargetAngle = Mathf.Atan2(m_Direction.x, m_Direction.z) * Mathf.Rad2Deg + m_Camera.transform.eulerAngles.y;
    11.                 float m_Angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, m_TargetAngle, ref m_TurnSmoothVelocity, m_TurnSmoothTime);
    12.                 transform.rotation = Quaternion.Euler(0f, m_Angle, 0f);
    13.  
    14.                 m_MoveDirection = Quaternion.Euler(0f, m_TargetAngle, 0f) * Vector3.forward;
    15.                 m_MoveDirection = m_MoveDirection.normalized;
    16.             }
    17.  
    18.             m_Animator.SetBool("IsGrounded", m_IsGrounded);
    19.             m_Animator.SetFloat("FallTime", m_FallTime);
    20.  
    21.             m_Animator.SetFloat("Speed", m_Speed, m_SpeedSmoothDamp, Time.deltaTime);
    22.             m_Animator.SetFloat("Horizontal", m_MoveDirection.x, m_DirectionSmoothDamp, Time.deltaTime);
    23.             m_Animator.SetFloat("Vertical", m_MoveDirection.z, m_DirectionSmoothDamp, Time.deltaTime);
    24.  
    25.             Sprint();
    26.             CalculateFalltime();
    27.             CalculateStamina();
    28.  
    29.             // Set Gravity to the Player if he is Falling
    30.             if (m_IsFalling) {
    31.                 m_Velocity.y -= m_Gravity * Time.deltaTime;
    32.                 m_CharacterController.Move(m_Velocity * Time.deltaTime);
    33.                 m_IsFalling = !m_IsGrounded;
    34.             }
    35.             else {
    36.                 m_CharacterController.Move(m_RootMotion + Vector3.down * m_StepDown);
    37.                 m_RootMotion = Vector3.zero;
    38.  
    39.                 if (!m_IsGrounded) {
    40.                     m_IsFalling = true;
    41.                     m_Velocity = m_Animator.velocity;
    42.                     m_Velocity.y = 0;
    43.                 }
    44.             }
    45.  
    46.             m_IsGrounded = m_CharacterController.isGrounded;
    47.         }
     
  5. Salja

    Salja

    Joined:
    Mar 23, 2017
    Posts:
    353
    Thanks looks like it works smooth now but now i got a other issue

    Code (CSharp):
    1. m_IsGrounded = m_CharacterController.isGrounded;
    Now this line makes problems, if i put this into the update and go in playmode my Player starts falling for 2 secs before he detects that he's on the ground



    It confuses me a lot, maybe they can put me on the right track, in fixed update before works he stay after press play at the ground without falling