Rigitbody movement in camera direction

Hello, i need to adjust player movement to the camara, so when i move with my camere and pres “W” forward the player is going forward the postioton where camera is pointing.

My Player CODE:

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

public class Player : MonoBehaviour
{

[SerializeField] private Transform groundCheckTransform = null;

[SerializeField] private LayerMask playerMask;

private bool jumpKeyWasPressed;

private float horizontalInput;
private float verticalInput;

private Rigidbody rigidbodyComponent;

private int superJumpRemaining;

private float minFov = 15f;
private float maxFov = 50f;
private float sensitivity = 10f;

void Start()
{
rigidbodyComponent = GetComponent();

}

void Update()
{

if (Input.GetKeyDown(KeyCode.Space))
{
jumpKeyWasPressed = true;
}

horizontalInput = Input.GetAxis(“Horizontal”);
verticalInput = Input.GetAxis(“Vertical”);

float fov = Camera.main.fieldOfView;
fov += Input.GetAxis(“Mouse ScrollWheel”) * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
}

private void FixedUpdate()
{

rigidbodyComponent.velocity = new Vector3(horizontalInput, rigidbodyComponent.velocity.y, verticalInput);

//
if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f, playerMask).Length == 0)
{
return;
}

//
if (jumpKeyWasPressed)
{
float jumpPower = 6;

if(superJumpRemaining > 0)
{
jumpPower *= 2;
superJumpRemaining–;
}
rigidbodyComponent.AddForce(Vector3.up * jumpPower, ForceMode.VelocityChange);
jumpKeyWasPressed = false;
}
}

//
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.layer == 7)
{
Destroy(other.gameObject);
superJumpRemaining++;
}
}

}

My CAMERA CODE:

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

public class CameraMovementt : MonoBehaviour
{
[SerializeField] private Camera cam;
[SerializeField] private Transform Player;
[SerializeField] private float distanceToTarget = 10;

private Vector3 previousPosition;

private void Update()
{
if (Input.GetMouseButtonDown(0))
{
previousPosition = cam.ScreenToViewportPoint(Input.mousePosition);
}
else if (Input.GetMouseButton(0))
{
Vector3 newPosition = cam.ScreenToViewportPoint(Input.mousePosition);
Vector3 direction = previousPosition - newPosition;

float rotationAroundYAxis = -direction.x * 180; // camera moves horizontally
float rotationAroundXAxis = direction.y * 180; // camera moves vertically

cam.transform.position = Player.position;

cam.transform.Rotate(new Vector3(1, 0, 0), rotationAroundXAxis);
cam.transform.Rotate(new Vector3(0, 1, 0), rotationAroundYAxis, Space.World);

cam.transform.Translate(new Vector3(0, 0, -distanceToTarget));

previousPosition = newPosition;

}
}
}

You can access the direction the camera is facing with transform.forward: Unity - Scripting API: Transform.forward

You obviously want to get rid of the y-axis component when doing this. So if (x,y,z) is the direction your camera is facing, you want to move in the direction (x,0,z).normalized, and multiply that by some speed.

Thank you, its my first time coding something, i will try to do it, apriciate the help.

Also, FYI you should use code-tags when posting code because plain-text is very difficult to read.

Thank you very much for information, i will use that later.