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): using System.Collections; using System.Collections.Generic; using UnityEngine; using StriXInteractive.Input; namespace StriXInteractive.PlayerController { public class PlayerController_v2 : MonoBehaviour { #region Vabrialen public CharacterController m_CharacterController; public InputManager m_Input; [Tooltip("Set Players Gravity")] public float m_Gravity = 20f; public float m_StepDown = 0.3f; public float m_DirectionSmoothDamp = 0.25f; public float m_SpeedSmoothDamp = 0.15f; public float m_TurnSmoothTime = 0.1f; public KeyCode m_SprintKey = KeyCode.LeftShift; private float m_Speed; private float m_SpeedFactor = 0.5f; private float m_FallTime; private float m_TurnSmoothVelocity; private float m_Horizontal; private float m_Vertical; private Animator m_Animator; private Camera m_Camera; private Vector3 m_RootMotion; private Vector3 m_Velocity; private Vector3 m_Direction; private Vector3 m_MoveDirection; private bool m_IsGrounded; private bool m_IsFalling; private bool m_IsSprinting; public float m_Stamina; public float m_MaxStamina = 10f; public float m_Health; public float m_MaxHealth = 100f; #endregion #region Start private void Start() { m_Animator = this.GetComponent<Animator>(); m_Camera = Camera.main; } #endregion #region Update private void Update() { m_Horizontal = m_Input.GetHorizontalRaw(); m_Vertical = m_Input.GetVerticalRaw(); m_Direction = new Vector3(m_Horizontal, 0, m_Vertical).normalized; m_Speed = m_Direction.magnitude * m_SpeedFactor; if (m_Speed >= 0.2f) { float m_TargetAngle = Mathf.Atan2(m_Direction.x, m_Direction.z) * Mathf.Rad2Deg + m_Camera.transform.eulerAngles.y; float m_Angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, m_TargetAngle, ref m_TurnSmoothVelocity, m_TurnSmoothTime); transform.rotation = Quaternion.Euler(0f, m_Angle, 0f); m_MoveDirection = Quaternion.Euler(0f, m_TargetAngle, 0f) * Vector3.forward; m_MoveDirection = m_MoveDirection.normalized; } m_Animator.SetBool("IsGrounded", m_IsGrounded); m_Animator.SetFloat("FallTime", m_FallTime); m_Animator.SetFloat("Speed", m_Speed, m_SpeedSmoothDamp, Time.deltaTime); m_Animator.SetFloat("Horizontal", m_MoveDirection.x, m_DirectionSmoothDamp, Time.deltaTime); m_Animator.SetFloat("Vertical", m_MoveDirection.z, m_DirectionSmoothDamp, Time.deltaTime); Sprint(); CalculateFalltime(); CalculateStamina(); } #endregion #region FixedUpdate private void FixedUpdate() { // Set Gravity to the Player if he is Falling if (m_IsFalling) { m_Velocity.y -= m_Gravity * Time.fixedDeltaTime; m_CharacterController.Move(m_Velocity * Time.fixedDeltaTime); m_IsFalling = !m_IsGrounded; } else { m_CharacterController.Move(m_RootMotion + Vector3.down * m_StepDown); m_RootMotion = Vector3.zero; if (!m_IsGrounded) { m_IsFalling = true; m_Velocity = m_Animator.velocity; m_Velocity.y = 0; } } m_IsGrounded = m_CharacterController.isGrounded; } #endregion #region OnAnimatorMove private void OnAnimatorMove() { m_RootMotion += m_Animator.deltaPosition; } #endregion #region Functions public float CalculateFalltime() { // Calculate Player Falltime return m_FallTime = !m_IsGrounded ? m_FallTime += Time.deltaTime * 1.0f : 0.0f; } public void Sprint() { // Check if the Sprint Input is pessed bool m_InputPressed = m_Input.GetKey(m_SprintKey); // return true if the Input is pressed and the player speed is greather then 0.2 m_IsSprinting = m_InputPressed && m_Speed > 0.2f ? true : false; // Set the Speedfactor if the Player press the Sprinting Input // Check if the Player has Stamina for Sprinting m_SpeedFactor = m_IsSprinting && m_Stamina > 0.0f ? m_SpeedFactor = 1.0f : m_SpeedFactor = 0.5f; m_Speed = Mathf.Lerp(m_Speed, m_SpeedFactor, Time.deltaTime); } public void CalculateStamina() { // Calculate Stamina for Sprinting if (m_IsSprinting) { if (m_Stamina > 0) { m_Stamina = m_Stamina - 2.0f * Time.deltaTime; } else { m_Stamina = 0; } } else { if (m_Stamina < m_MaxStamina) { m_Stamina = m_Stamina + 0.5f * Time.deltaTime; } else { m_Stamina = m_MaxStamina; } } } #endregion } }
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): private void Update() { m_Horizontal = m_Input.GetHorizontalRaw(); m_Vertical = m_Input.GetVerticalRaw(); m_Direction = new Vector3(m_Horizontal, 0, m_Vertical).normalized; m_Speed = m_Direction.magnitude * m_SpeedFactor; if (m_Speed >= 0.2f) { float m_TargetAngle = Mathf.Atan2(m_Direction.x, m_Direction.z) * Mathf.Rad2Deg + m_Camera.transform.eulerAngles.y; float m_Angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, m_TargetAngle, ref m_TurnSmoothVelocity, m_TurnSmoothTime); transform.rotation = Quaternion.Euler(0f, m_Angle, 0f); m_MoveDirection = Quaternion.Euler(0f, m_TargetAngle, 0f) * Vector3.forward; m_MoveDirection = m_MoveDirection.normalized; } m_Animator.SetBool("IsGrounded", m_IsGrounded); m_Animator.SetFloat("FallTime", m_FallTime); m_Animator.SetFloat("Speed", m_Speed, m_SpeedSmoothDamp, Time.deltaTime); m_Animator.SetFloat("Horizontal", m_MoveDirection.x, m_DirectionSmoothDamp, Time.deltaTime); m_Animator.SetFloat("Vertical", m_MoveDirection.z, m_DirectionSmoothDamp, Time.deltaTime); Sprint(); CalculateFalltime(); CalculateStamina(); // Set Gravity to the Player if he is Falling if (m_IsFalling) { m_Velocity.y -= m_Gravity * Time.deltaTime; m_CharacterController.Move(m_Velocity * Time.deltaTime); m_IsFalling = !m_IsGrounded; } else { m_CharacterController.Move(m_RootMotion + Vector3.down * m_StepDown); m_RootMotion = Vector3.zero; if (!m_IsGrounded) { m_IsFalling = true; m_Velocity = m_Animator.velocity; m_Velocity.y = 0; } } m_IsGrounded = m_CharacterController.isGrounded; }
Thanks looks like it works smooth now but now i got a other issue Code (CSharp): 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