0
\$\begingroup\$

I have a GameObject called Player with a CharacterController component attached that has the following settings.

enter image description here


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.

enter image description here

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. enter image description here

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.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Generally the best way to handle a character on any sort of moving platform is to make the character object a child of whatever it is standing on.

That way, movements of the platform will perfectly translate into movements of the character.

Just remember to unparent them when the character is no longer supported by the platform.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .