Make player follow boat rotation

Hello everyone !

I am very new in coding and unity. I have some trouble making my player position following the boat rotation.
For now:
My player can move with Input.GetAxis AND follow the boat position, so if the boat go straight forward there is no problem with that.
If I give a rotation to the boat ( with the Helm object) the boat do a rotation with the helm object as pivot, but the player do not update his position with this rotation…

I would like the player to move freely on the boat as he does already, and his

position to also update with the rotation given to the boat, or just that it look like he did.

Here is one solution I tried,

Calculating the distance between player and helm, and when the player give a rotation to the boat, this rotation is apply the player given is distance to the helm

PlayerBoatRotationIssue

Here is some of my code for BoatController:

public float helmDirection { get; private set; }

        float helmDirection = 0f;

        helmDirection = (helmAngle.localEulerAngles.y / 180) - 0.5f;

        // Rotation du bateau
        transform.Rotate(Vector3.up * rotateSpeed * helmDirection * Time.deltaTime * -1, Space.Self);

        // Direction du mouvement
        v3Direction = -1f * transform.forward;

        // Normaliser la direction
        if (v3Direction.magnitude > 1)
        {
            v3Direction.Normalize();
        }

        // Calcul du déplacement

    v3Move = v3Direction * currentSpeed * Time.deltaTime;
    rb.MovePosition(rb.position + v3Move); // Utiliser MovePosition pour un mouvement plus fluide

    // Transmettre le mouvement au joueur
    boatMove = v3Move;
    }

And some of my script for Player_Controller:

   {if (dead == false)
        {
            SC_boatControllerRigidBody boatController = GameObject.FindWithTag("Boat").GetComponent<SC_boatControllerRigidBody>();
            boatMove = boatController.boatMove; 

        if (isImmune)
        {
                    fTimerImmunity += Time.deltaTime;
                    
                    if (fTimerImmunity >= 3f)
                    {
                        isImmune = false;
                        fTimerImmunity = 0f;
                    }
        }


            if (isMoving)
            {


                v3Direction = new Vector3(Input.GetAxis("Horizontal"), 0, -Input.GetAxis("Vertical"));

                if (v3Direction.magnitude > 1)
                    v3Direction.Normalize();

                Vector3 movement = v3Direction * fSpeed * Time.deltaTime;

                // Applique le mouvement et la rotation
                controller.Move(movement + boatMove);

                // Applique la rotation du bateau
                transform.rotation = boatController.boatRotation;

            }
            else if(isMoving == false)
            {
                controller.Move(boatMove);
                transform.rotation = boatController.boatRotation;                    
            }

Note that I tried with the help of CHATGPT I admit :slight_smile: but I didn’t really like his solution.

When trying to create a new Vector , that calculate the distance to helm + boat rotation
and applying in for example

controller.Move(movement + boatMove + offset)

the offset calculated was making my player goes far offscreen

Anyway, don’t hesitate to tell me if you need more info !

And thank you for your help :slight_smile:

PS: looking for alternative solution also

You can get the velocity of the boat’s surface below the player using this.

You’ll need to rotate the boat with AddTorque or set it to be kinematic and use MoveRotation.

you could parent the player to the boat game object. it should turn fine but it will probably double the movement of going forward. so you would have to disable the script or part of the script that moves the players position the same amount of the ships position.

in the player_controller script.

controller.Move(movement + boatMove); //you should change this to controller.Move(movement); and
parent the player to the boat.