How do you move a rigidbody relative to it's rotation

yo waddup, I’m fairly new to coding and trying to make a first person character using the rigidbody component but I’m not sure how to get it to move based on it’s rotation, currently it’s moving based on world coordinates. I’ll have my current code attached. I would also appreciate it if I could get an explanation as to what’s going on rather than simply a block of code that works.

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

public class RigidbodyMovement : MonoBehaviour
{
    public float speed = 5f;
    Rigidbody playerRigidbody;

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

    // Update is called once per frame.
    void Update()
    {
       
        Vector3 userInput = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        playerRigidbody.MovePosition(transform.position + userInput * Time.deltaTime * speed);

    }
}

Hi,

userInput is represented as a local (to the character) movement vector (XZ movement), so you need to convert that from local to world coordinates based on the current character orientation. One way to achieve this is like this:

Vector3 userInput = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
userInput  =
       transform.right * userInput.x +
       transform.up * userInput.y +
       transform.forward * userInput.z;

Another way to do the same would be to use TransformVector or TransformDirection. Be careful, TransformVector gets affected by scale, which is not something that you’d normally want. Use TransformDirection instead:

Vector3 userInput = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
userInput  = transform.TransformDirection(userInput);

Thanks heaps, I’ve implemented the first one and it works a treat, and I’m pretty sure I understand what’s going on as well