How to make character move in the direction its facing

I’m trying to make my character move forwards in the way that the camera is facing, but it doesn’t seem to work. I’ve tried multiple solutions in which none have worked. it still moves on the world space.

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

public class Movement : MonoBehaviour
{
    public Transform Player;
    public Transform Head;
    public Rigidbody rb;
    public Camera camera;
    private float speed = 20.0f;
    private float jumpforce = 1.0f;
    public bool CanJump = true;


    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.D))
        {
            transform.position += transform.right * speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.A))
        {
            transform.position -= transform.right * speed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.Space))
        {
            if (CanJump == true)
            {
                rb.velocity = new Vector3(0, 5, 0);
            }
        }

        if (Input.GetKey(KeyCode.W))
        {
            transform.position += transform.forward * Time.deltaTime * speed;
        }

        if (Input.GetKey(KeyCode.S))
        {
            transform.position -= transform.forward * speed * Time.deltaTime;
        }
    }
    
    void OnCollisionEnter(Collision collision)
    {
        CanJump = true;
    }

    void OnCollisionExit(Collision collision)
    {
        CanJump = false;
    }
}

Hi @LyraxH,

I assume the script is attached to the player object? Then transform.forward (or any of the transform.X) is using the players transform. If you want to get the looking direction of the camera, use Camera.main.transform.forward.

Note: I think querying Camera.main was a bit performance heavy, so save it in a variable