I have a GameObject called Player with a CharacterController component attached that has the following settings.
Player also has a script called PlayerController
attached.
Gravity is applied to the CharacterController through the following function, which is called in Update()
.
private void ApplyGravity()
{
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
}
The Player has a child GameObject called PlayerModel which just has a default CapsuleCollider attached.
The PlayerController
has an additional function called MoveAround()
, which is also called in Update()
; incase this is related to the issue.
public void MoveAround()
{
Vector2 moveInput = playerInputActions.Player.Move.ReadValue<Vector2>();
currentInput = new Vector2(moveInput.y, moveInput.x);
float moveDirectionY = moveDirection.y;
float speed = isSprinting ? sprintSpeed : walkSpeed;
moveDirection = (transform.forward * currentInput.x + transform.right * currentInput.y) + boatController.boatRigidbody.velocity;
moveDirection.y = moveDirectionY;
moveDirection.Normalize();
moveDirection *= speed;
}
I have a GameObject called Boat, which has a default BoxCollider, Rigidbody and BoatController
script attached;
the Rigidbody has the following settings.
The Boat is floating on water, therefore moving up and down.
The Player is resting on the surface of the Boat.
When the Boat floats up, the Player is pushed upwards with it. When it floats back down, the ApplyGravity()
function is called, bringing the Player back down with it.
The Scene setup looks like this.
The problem is this process is not smooth.
The Player jitters at the collision point when being pushed up, and when falling back down.
I have attempted trying to snap the Player position to the surface of the Boat, however this causes the same issue.
Building on from that, I have also tried using the combination of Physics.Raycast()
and Vector3.Lerp()
to grab the surface position and smoothly move the Player to that position respectively; this caused some strange bugs (but I could've implemented it incorrectly).
Any advice on how to solve this is much appreciated.