I want to get the direction that the player is moving in relative to the camera, So for example if they’re moving right then the game will recognize that. I could just detect if they’re pressing the A or D buttons, But that wouldn’t work with anything that has a joystick.
I want the exact direction they’re heading relative to the camera position, But I haven’t been able to figure it out.
I’ve looked around the forum but couldn’t find anything for this specific issue.
I’ve tried this, But it seems to have weird results, And I’m not sure how to detect if you’re going left or right with this. It appears to change if you move which axis you’re moving on, rather than if you’re going left or right from the camera.
No, Doesn’t work. It seems to have the same issue. I need it to calculate the direction in local space relative to the camera. I may be able to get the input directly from the gamepad and just detect the direction the player is moving depending on the keys being pressed for keyboard controls, So this may not be necessary at all.
If you take the Unit of the above Vector, you will get the direction they are moving relative to the camera; defined by the direction of x/y/z with a magnitude of 1. We simply don’t use the Y, since we aren’t checking if they are moving up and down in this case.
local movementDirection = camera.CFrame:vectorToObjectSpace(humanoid.MoveDirection).Unit
if math.abs(movementDirection.X) > math.abs(movementDirection.Z) then
-- if x is negative then character is moving left, if its positive then they are moving right (in relation to camera)
elseif math.abs(movementDirection.X) < math.abs(movementDirection.Z) then
-- if z is negative then character is moving forward, else they are moving backward (in relation to camera)
else
-- not moving (or moving perfectly diagonally, but unlikely, may want to add a condition for that as well).
end
Note that you can incorporate both the x and z values to detect diagonal movement as well. If you need more than just the four cardinal directions, I can explain that as well.
Apologies, I know this thread is around 5 years old, but I was wondering if you’d still be up for explaining how to apply this to eight directions rather than just four.
To determine the direction a player is you could use the player’s movement vector and the cameras orientation. Then use these values to calculate the movement direction, using the camera orientation is probably important to you as depending on if the camera is facing the front, back or side of the player would give a different result.
Get the player’s movement vector
Get the camera’s rotation vector
Calculate the direction based on the projection which will be relative to the camera
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera
game:GetService("RunService").RenderStepped:Connect(function()
--Movement direction
local moveDirection = humanoidRootPart.Velocity
if moveDirection.Magnitude > 0.1 then --Check player is moving
--Get cameras CFrame
local camCF = camera.CFrame
local camForward = camCF.LookVector
local camRight = camCF.RightVector
local movement = moveDirection.Unit
--Project movement onto camera position
local relativeX = movement:Dot(camRight)
local relativeZ = movement:Dot(camForward)
--Calculate the direction
if math.abs(relativeX) > math.abs(relativeZ) then
if relativeX > 0 then
print("Moving right relative to camera")
else
print("Moving left relative to camera")
end
else
if relativeZ > 0 then
print("Moving forward relative to camera")
else
print("Moving backward relative to camera")
end
end
end
end)