Need help with translating camera rotation to directional Vector3

So I’m trying to make my own movement system using the camera and a BodyVelocity object, but for some reason it’s not working correctly and I can’t wrap my head around why.

I’m trying to use the Pythagorean theorem to calculate the X and Z velocity, using the camera’s Y rotation as theta. Here’s the script:

local cam = workspace.Camera

...

-- Everything below this line fires whenever the W key is pressed.
local rot = Vector3.new(cam.CFrame:ToEulerAnglesXYZ()).Y
local xVel = math.sin(rot) * 16
local zVel = math.cos(rot) * 16
walkForce.Velocity = Vector3.new(-xVel, 0, -zVel)

I know I’m definitely doing something wrong here because whenever I rotate the camera along the X/Z axes, it affects the output of both xVel and zVel. I’m gonna assume from that alone that there’s probably some 3D wizardry going on that I can’t grasp (or I’m probably just stupid)

2 Likes

Can you give some game play for context?

1 Like

Here you can see me pressing the W key to try and change the direction of the character’s movement. It seems to work somewhat fine on the x-axis (until I point the camera above the character) but the character continuously moves forward on the z-axis no matter where I point the camera.

(apologies for the low framerate btw, I was using the built-in video recorder)