Getting the angle that the player is moving based off of the camera

Hey, I’m currently trying to make a player movement system which always has the mouse/camera locked in the center of the screen. I’ve searched for a bunch of solutions to making the player angled toward where they are moving to (almost like strafing), I know it has something to do with the humanoid’s MoveDirection and the camera’s lookvector. But my problem is that I don’t know how to get the angle the player is moving from those two properties.

5 Likes

The angle between 2 vectors a and b is:

math.acos(a.Unit:Dot(b.Unit))

Be aware this can be NaN if the player isn’t moving, since MoveDirection would have a magnitude of 0.

1 Like

With that method the angle you get will always be positive, so you can know what degree the player is moving sideways but not which direction. Using atan2 will give you a positive number if they are strafing left and negative if they are strafing right:

-- Get a transform of MoveDirection relative to the Camera orientation:
-- (Subtract move direction instead of add because cframe lookvectors are in the negative Z direction)
local relativeDirection = workspace.Camera.CFrame:PointToObjectSpace(workspace.Camera.CFrame.Position - humanoid.MoveDirection)

if (relativeDirection.Magnitude > 0.0001) then
	local angle = math.atan2(relativeDirection.X, relativeDirection.Z)
	print("angle is:", angle)
else
	print("not moving")
end

How would I be able to use the “angle” variable as a property inside of a CFrame Orientation. I’m basically trying to get the player to look in the direction they are moving even in shiftlock or first person.