Constant movement on z axis while following the rotation on y

So ive been fooling around for a while now following walkalongs and what so ever but keep drifting away. Im now focused on creating my game i have had in my head for a long while.

I have created movement. when i press wasd it rotates in the angle i want to face and it works fine and set up to be camera relative.

Next i set up forward movement on 1,2,3 1 stops the boat, 2 moves the boat slow, and 3 moves it fast.

What i am trying to do is have it to seem like 1 the sails are up, 2 is half and 3 is full sails. Im trying to get the boat to move forwards following the direction that the boat has as its local z axis.
currently it moves along the z axis, rotates but doesnt follow the z axis until i press the 1,2,3 button. i want it to always follow the z axis when moving.

below is the script im using. any help would be welcome.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class accelerateandsteer : MonoBehaviour
{
    Rigidbody rb;
    public float speed = 5f;
    public float fasterSpeed = 6f;
    public float speedIncrements = 2f;
    public float rotationSpeed = 50f;
    public float turnSmothing = 0.1f;
    float turnSmoothVelocity;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();            
    }

    // Update is called once per frame
    void Update()
    {
        Rotation();
        Acceleration();
    }
    
    private void Acceleration()
    {
        if (Input.GetKey("1"))
        {
            rb.velocity = transform.forward * speed * 0f * Time.deltaTime;
          Debug.Log("1 Pressed");
        }
        if (Input.GetKey("2"))
        {
            float horizontal = Input.GetAxisRaw("Horizontal");
            float vertical = Input.GetAxisRaw("Vertical");
            Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
            if (direction.magnitude >= 0.1f)
            {
                float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
                float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmothing);
                transform.rotation = Quaternion.Euler(0f, angle, 0f);
            }
            rb.velocity = transform.forward * speed * 100f * Time.deltaTime;
            Debug.Log("2 Pressed");
        }
        if (Input.GetKey("3"))
        {
            rb.velocity = transform.forward * fasterSpeed * 100f * Time.deltaTime;
            Debug.Log("3 Pressed");
        }
    }

    private void Rotation()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = new Vector3 (horizontal, 0f, vertical).normalized;
        if(direction.magnitude >= 0.1f)
        {
            float targetAngle = Mathf.Atan2(direction.x,direction.z) * Mathf.Rad2Deg;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmothing);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
        }
        
    }


}

Privacy & Terms