Move Player according to Cam Direction

Got this game I’m working on with a classmate. He had done the prior code for the cam and movement.
Our problem is that the movement direction is always the same no matter the camera direction.
Because I’m new to unity I got no clue how to fix it and he is also new. When checking other places I can’t find similar code so I’m not sure how to fix it.

if (direction.magnitude >= 0.1f) //A check to see if the player is moving.
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y; //Points the player to the direction that they are moving.
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime); //Uses the direction to figure out how much the player should rotate on the Y axis.
transform.rotation = Quaternion.Euler(0f, angle, 0f); //Sets the rotation.

Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward; //Makes the player move in the direction that they are moving.
controller.Move(direction * speed * Time.deltaTime); //Determines how fast the player is going.
}

This Is the part of the code that takes care of the camera but I just have a hard time understanding it and coming up with a fix. Thanks in advance!

Hello.

Its ok to come here to ask for a Unity prroblem, but this is a very basic issue, you still need to learn about Local/World Space references. I strongly recommend to learn / look for tutorials about:

-Local reference vs World reference

-Vector3 from otherObject LocalSpace reference

-Transform.Translate

-Transform.Forward

Good Luck!
Bye!