How to make the character move in the direction the camera is facing?

So I am using cinemachine for my third-person camera and I got the camera movement part down, but if I move the camera the player doesn’t move in the way the camera is facing and that would make the character really hard to control if the player isn’t facing the way the camera. I don’t know where to start when it comes to stuff like this so help would be nice.

my playerMovement script:

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

public class BallController : MonoBehaviour
{
    public float speed = 5f;
    private Rigidbody rigid;
    public float UpForce = 500f;
    private bool isGrounded;
    public Rigidbody rb;
    public Transform cam;

    private void Start()
    {
        rigid = gameObject.GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        if (Input.GetAxis("Vertical") < 0) 
        {
            rigid.AddForce(-Vector3.left * speed);
        }
        else if (Input.GetAxis("Vertical") > 0)
        {
            rigid.AddForce(Vector3.left * speed);
        }

        if (Input.GetAxis("Horizontal") > 0)
        {
            rigid.AddForce(Vector3.forward * speed);
        }
        else if (Input.GetAxis("Horizontal") < 0)
        {
            rigid.AddForce(-Vector3.forward * speed);
        }

        if (rb.position.y < -1f)
        {
            FindObjectOfType<GameManager>().EndGame();
        }
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && !isGrounded)
        {
            rigid.AddForce(Vector3.up * UpForce);
            Debug.Log("Jumped");
            isGrounded = true;
        }

    }

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }
   
    
}

So what you’re using is Rigidbody.AddForce and what this does is applies a force on the rigidbody on the vector that you specificed (forward, back, left, right, up, down). But the rotation of the rigidbody has to change to face in the direction of the camera.
*
rigid.rotation = cam.rotation;

In you movement script you have to add a rotation to the rigidbody on the y axis the best way you can use is.
If you need that the player rotates only when is moved you have to check insert this code in a if condition like
if(input.horizontal || input.vertical != 0) i advice you to create a Vector2 with this parameters. If you didnt understood tell me :wink:

Rotate player in camera direction regardless

rigid.MoveRotation(Quaternion.Euler(0, cam.eulerAngles.y, 0));





Rotate player in camera direction if moved

Vector2 inputDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

if (inputDirection != Vector2.zero)
{ rigid.MoveRotation(Quaternion.Euler(0, cam.eulerAngles.y, 0)); }