- Home /
Character controller jittering up and down
Hi people of the internet. I have an issue where my character jitter up and down when moving. I use the following code to control it (third person controller) on a character with a capsule collider and despite my efforts the jitter just wont stop. I tried changing the position of the gravity calculation but nothing.Its quite annoying. Please tell me the way to smooth movement.
private void Update()
{
GetInput();
CalculateDirection();
CalculateForward();
RaycastGround();
if (Input.GetButtonDown("Jump"))
{
Jump();
}
}
private void FixedUpdate()
{
if (m_Input != Vector3.zero)
{
m_Animator.SetBool("IsMoving", true);
Rotate();
SetSpeed();
}
else
{
m_Animator.SetBool("IsMoving", false);
ResetSpeed();
}
m_MoveDir = m_Forward * m_CurrentSpeed;
m_MoveDir.y = m_RigidBody.velocity.y;
if (m_RigidBody.velocity.y > 1)
{
}
m_MoveDir.y -= m_FakeGravity * Time.fixedDeltaTime;
m_RigidBody.velocity = m_MoveDir;
}
private void GetInput()
{
m_Input.x = Input.GetAxis("Horizontal");
m_Input.z = Input.GetAxis("Vertical");
}
private void CalculateDirection()
{
m_RotationAngle = Mathf.Atan2(m_Input.x, m_Input.z);
m_RotationAngle *= Mathf.Rad2Deg;
m_RotationAngle += m_Camera.eulerAngles.y;
}
private void CalculateForward()
{
m_Forward = transform.forward;
}
private void RaycastGround()
{
m_IsGrounded = Physics.Raycast(transform.position, Vector3.down, out m_HitInfo, m_CharacterHeight + m_HeightPadding, m_WalkLayer);
}
private void Rotate()
{
m_TargetRotation = Quaternion.Euler(0f, m_RotationAngle, 0f);
transform.rotation = Quaternion.Slerp(transform.rotation, m_TargetRotation, m_TurnSpeed * Time.fixedDeltaTime);
}
private void SetSpeed()
{
if (m_CurrentSpeed < m_RunSpeed)
{
m_CurrentSpeed += m_RunSpeed * (m_RunAcceleration * Time.fixedDeltaTime);
}
else
{
ResetSpeed();
}
}
private void Jump()
{
if (m_IsGrounded)
{
m_IsGrounded = false;
m_RigidBody.AddForce(transform.up * m_JumpForce);
}
}
private void ResetSpeed()
{
m_CurrentSpeed = 0f;
}
Answer by Tomatotom1234 · Jan 07, 2021 at 05:30 PM
try using the character controller in unity its much better anyways
Answer by MomkeyDev · Jan 08, 2021 at 03:02 PM
Yeah tom right, character controller is the one that can smoothly traverse slope (Also traverse stair too infact) but the only downside is no rigidbody and no gravity. Don't worry though, brackey fps tutorial can help you with it. Anyway, why this happen ? Rigidbody. When dealing with slopes, Rigidbody either make you jump high (if you use addforce) or jitter movement (if you changes the transform) but if you still forcing to use Rigidbody, you can try to use the interpolation settings, it's in the Rigidbody option. Not recommend tho