Help Script: Player should follow boat

Hello there,

I am new to Unity and Scripting, but I had this idea test if I was able to make a Sailing Boat work ( with help to chat GPT for the boat )

I made it mostly work but I have this big difficulty: The player don’t follow the boat

I would like the player to stay on the boat, and move freely in the boat

I notice that when I immobilized the player while being in interaction with the the “SailController” the player follow the boat. But as soon The player is not interacting anymore and can move freely, it dosen t follow anymore…

i tried parenting the player but didn’t work

If someone can help me with that it would be great !

Here is my playerController Code: ( some part )

void Start()
{
// Initialisation des composants
controller = GetComponent();
animator = GetComponent();
Camera.main.GetComponent<SC_Life>().LifeUpdate(Life);

    // Initialisation de la position locale
    if (boat != null)
    {
        localPosition = transform.position - boat.position;
    }
}

void Update()
{
    if (!dead)
    {
        Timer += Time.deltaTime;

        if (isOnBoat && boat != null)
        {
            // Mettre à jour la position locale du joueur
            if (canMove)
            {
                float horizontalInput = Input.GetAxis("Horizontal");
                float verticalInput = Input.GetAxis("Vertical");
                Vector3 moveDirection = new Vector3(horizontalInput, 0, verticalInput);

                // Déplacer le joueur en fonction des entrées
                controller.Move(moveDirection * fSpeed * Time.deltaTime);

                // Mettre à jour la position locale relative au bateau
                localPosition = transform.position - boat.position;
            }

            // Mettre à jour la position du joueur sur le bateau
            transform.position = boat.position + localPosition;
        }
        else
        {
            // Mouvement normal du joueur hors du bateau
            if (canMove)
            {
                v3Direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

If someone knows a way :slight_smile: I really want to try making this litlle sailing boat work , thx !

It’s ok if the player cannot move outside the boat

Below line 21 you should add the boat’s velocity to the player’s moveDirection.

thx I 'm gonna try

You can also make an empty GameObject and make both the boat and player children of it, then move the parent instead of the boat (move it in the same way you moved the boat originally). The player would be free to move anywhere, including off the boat, so there may be restrictions needed.

Thx for the tip gonna try that ! ( I tried with the boat velocity solution but didn’t wok either )

really wish I make this work