How to get what direction player are walking

I mean like it print Left or Right or Forward or Backward without using KeyBegin so it works on mobile too

You could use Humanoid.MoveDirection.

Yeah but I can’t like make it able to see if its walking forward or backward etc it can be any number not like 1,0,0

Use Humanoid.MoveDirection:Dot(<character root part>.CFrame.LookVector).
0 means the player is moving sideways, 1 means the player is moving forward and -1 means the player is moving backwards, all relative to their root part.
If you were to base this on the camera direction, use Humanoid.MoveDirection:Dot(<camera>.CFrame.LookVector)
Again, 0 means the player is moving sideways, 1 means the player is moving forward, -1 means the player is moving backwards. This is relative to the camera as we use the camera instead of the root part of the character.

10 Likes

This works but if you change direction sometimes even walking forward it says sideways

This works but if you change direction sometimes even walking forward it says sideways

That’s because the character IS walking sideways relative to the world axis.
I’m assuming you want to get the character direction based off the direction of the camera, so something like this could help:

local cam = workspace.CurrentCamera -- getting camera
local humanoid = character:WaitForChild("Humanoid") -- getting humanoid

movementDirection = cam.CFrame:vectorToObjectSpace(humanoid.MoveDirection).Unit -- this gets the humanoid movement direction based on the camera rotation
7 Likes

Thanks this works but any idea why when I hold W and D it go to 0 in this script and when I hold W and A it go to -1?

local Humanoid = script.Parent.Parent:WaitForChild('Humanoid')

local Camera = workspace.CurrentCamera

local function MoveDirection()

return math.floor(Humanoid.MoveDirection:Dot(Camera.CFrame.XVector))

end

while wait() do

if tostring(MoveDirection()) == '-1' or tostring(MoveDirection()) == '-2' then

script.Parent:WaitForChild('KeyPressed'):FireServer('A', true)

else

script.Parent:WaitForChild('KeyPressed'):FireServer('A', false)

end

if tostring(MoveDirection()) == '1' or tostring(MoveDirection()) == '2' then

script.Parent:WaitForChild('KeyPressed'):FireServer('D', true)

else

script.Parent:WaitForChild('KeyPressed'):FireServer('D', false)

end

end

You need to do Camera.CFrame.LookVector instead of Camera.CFrame.XVector

function printDirection()
print(movementDirection)
end

–Mouse.KeyDown:Connect(printDirection) – for testing

– Dont add this, just for info

–[[

MoveDirection Unit type vector3

This is a unit vector that describes the movement
direction of the Humanoid.

For example when the Humanoid is moving forward,
MoveDirection will contain the vector (0,1,0).

–]]

(MoveDirection is a property of humanoid)

You could write a function using the previous MoveDirection value and the new one to find which co-ordinate changed.

Its because Im using it sideways

Yeah, but I need it to work on mobile 2

For detecting sideways movement?
In that case you can just check if the dot product is close to 0

Yeah but I need like Right & Left detection, not just check if walking sideways

Okay, then once we detect sideways movement is close to zero, you could use Humanoid.MoveDirection:Dot(.CFrame.RightVector).
1 means that we are moving right, -1 means we are moving left.
Make sure to add a little margin of error (for example, using < -0.9 instead of == -1)

4 Likes