The problem:
Player experiences jitter but only when the ship goes down. (singleplayer)
The video is attached below.
Context:
The ship is dynamic rigidbody.
The player uses character controller.
Code attached below
What I have tried:
Putting ApplyGravity() in LateUpdate()
Applying more downforce when grounded. (Turns out if I apply more, the jitter becomes even worse)
Switching to dynamic rigidbody (Built-in rigidbody gravity works fine but it doesn't suit my project cuz of multiplayer in the future)
Code:
The whole script:
https://paste.mod.gg/ognrfanalksl/0
Useful parts:
void LateUpdate()
{
RotatePlayerCharacter();
ApplyGravity();
}
private void AlignPositionWithShip()
{
// TODO separate the camera and smooth the Y movement
Vector3 delta = ship.GetDeltaPosition();
delta.y = 0;
characterController.Move(delta);
}
private void ApplyGravity()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckSphereRadius, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
else if (MovementMode.InWater == currentMovementMode)
{
// Reduced gravity effect in water
velocity.y = 0;
}
else
{
velocity.y += GRAVITY * Time.deltaTime ;
characterController.Move(velocity * Time.deltaTime);
}
}
private void Jump()
{
if (isGrounded)
{
velocity.y = Mathf.Sqrt(-2f * GRAVITY * jumpHeight);
}
}
A tool for sharing your source code with the world!