Turning movement forward being weird.

So, I made a script that turns u when u use the a & d keys, and then u can go forward. But if u turn it doesn’t go in the new forward it goes in the original forward. How do I fix this?

(code)

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
//rb.AddForce(0, 0, forwardForce * Time.deltaTime);
//if (Input.GetKey("space"))
public class Movement : MonoBehaviour
{
public Rigidbody rb;
    public float forwardForce = 1000f;
    public float turnForce = 50f;
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("w"))
        {
            rb.AddForce(0, 0, forwardForce * Time.deltaTime);
        }
        if (Input.GetKey("d"))
        {
            transform.Rotate(Vector3.right, turnForce * Time.deltaTime);
        }
        if (Input.GetKey("a"))
        {
            transform.Rotate(Vector3.right, -turnForce * Time.deltaTime);
        }
    }
}

transform.forward gives you the forward direction of the object that this script is attached to.
Also changed the transform.Rotate because it was not working on my end.


I added rb.AddTorque(); so it can turn correctly with the rigid body component.

public Rigidbody rb;
    public float forwardForce = 1000f;
    public float turnForce = 50f;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("w"))
        {
            rb.AddForce(transform.forward * forwardForce * Time.deltaTime);
        }
        if (Input.GetKey("d"))
        {
            rb.AddTorque(transform.up * turnForce * Time.deltaTime);
        }
        if (Input.GetKey("a"))
        {
            rb.AddTorque(transform.up * -turnForce * Time.deltaTime);
        }
    }