How to detect player's moving direction?

Hello developers,

I want to detect player’s moving direction but it’s hard to do.

I searched forums and got some solutions but it doesn’t work fully successful.

I used Vector3:Dot(), CFrame:VectorToObjectSpace(), etc.

I will use it in a first person game.

Any helps would be appreciated!

Yeah, I used that. But I think I went wrong. I used Camera.CFrame.LookVector:Dot(Humanoid.MoveDirection), and ControlModule:GetMoveVector(), but it doesn’t work as I expected.

how are you trying to use this in your code?

-- Returns the player's walking state.
local function PlayerWalkingState(MoveDirection: Vector3, CameraCFrame: CFrame, Humanoid: Humanoid): string
	if MoveDirection.Magnitude <= 0 or Humanoid:GetState() ~= Enum.HumanoidStateType.Running then return "Walk" end
	local HumanoidRootPart = Humanoid.RootPart
	if not HumanoidRootPart then
		return "Walk"
	end
	local Direction = ControlModule:GetMoveVector()
	local LookDirection = Direction.Z
	local RightDirection = Direction.X
	if RightDirection > 0 then
		if LookDirection > 0 then
			return "Left"
		else
			return "Right"
		end
	elseif RightDirection < 0 then
		if LookDirection > 0 then
			return "Right"
		else
			return "Left"
		end
	else
		return "Walk"
	end
end

can’t you just use the character’s velocity on something like their limbs and/or root part?

That makes sense, but I think Velocity can be incorrect though.

I believe Velocity got deprecated and AssemblyLinearVelocity is used instead

I meant AssemblyLinearVelocity and Velocity can give me incorrect speed because it can change if player rotates, or something.

whats the use case for getting their direction?

To slow player’s speed if they walk backward.

In PlayerModule.ControlModule.Keyboard I found where movement is handled, so you could probably see about adding in a way to control the Humanoid WalkSpeed using the action functions.

To get the modules, just play test and go into your PlayerScripts and it’ll be in there. Copy them, end the test, and paste them into StarterPlayerScripts and they’ll override.

But does it work if player is on mobile?

Unsure if its possible to do since the thumbstick changes walkspeed based on how much you pull it.

Is there a good way to do this that will work on all devices?

You’ll have to research more yourself

This post could help. How do I detect if a player is moving backwards?