Want to make player turn around using the mouse and the camera to always be behind the player

I tried to add some movements and having the camera follow the player. At first it looked like it worked, but then i realized that the directions are set to the buttons. If i turn around, it feels like everything is messed up. Left is right etc. I guessthe problem is the player is moved on the axis and that no part of the script senses which direction the player character is facing. How to solve it?

Script for controlling the camera with the mouse:

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

public class CameraFollowMouse : MonoBehaviour
{
    public float speedH = 2.0f;
    public float speedV = 2.0f;
    public float yaw = 0.0f;
    public float pitch = 0.0f;

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

    // Update is called once per frame
    void Update()
    {
        yaw += speedH * Input.GetAxis("Mouse X");
        pitch -= speedV * Input.GetAxis("Mouse Y");
        transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);

    }
}

Script for controlling the player:

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

public class PlayerMovement3D : MonoBehaviour
{

    private Rigidbody rb;

    // Moves the object forward at two units per second.
    public float speed = 2;
    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }
    void Update()

    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        Vector3 movement = new Vector3(x, 0, z);
        movement = Vector3.ClampMagnitude(movement, 1);
        transform.Translate(movement * speed * Time.deltaTime);
    }

}

Transforms, local space, world space, rotations, vectors:

If that doesn’t float your boat, you may wish to consider using Cinemachine from the Unity Package Manager.

Thanks for the reply!
I read the post and I’m still wondering. Maybe I misunderstood something but I couldn’t find any information on how to make movement direction change depending on which direction the character is facing.

I tried adding the lines below to the script but it just filled up with errors.

Vector3.forward * Transform.rotation;
Vector3.up * Transform.rotation;
Vector3.right * Transform.rotation;

Oh that’s cool and easy… I even have a setup to demo it!

This is the codelet:

https://github.com/kurtdekker/proximity_buttons/blob/master/proximity_buttons/Assets/DemoCameraRotatedControls/RotatedControlsPlayerController.cs

See lines 183 to 189 above.

And the full project is right there to download.