Checking Which Direction A Player Moves

Yeah but if MoveDirection.X is -1203808123e-3 and MoveDirection.Z is 1 or -1 then how am I supposed to check which direction the player’s moving?

I’m pretty sure you can use toObjectSpace and/or toWorldSpace to accomplish this.

Also, I thought you meant WalkToPoint. This is much more reliable for this.

1 Like

WalkToPoint always prints Vector3.new(0,0,0) no matter where you move.

But you can use that to see if they are walking left or right. Forwards or backwards.

Maybe you can use Character.PrimaryPart.Velocity.Unit

Something like this

local Player = game:GetService("Players").LocalPlayer
local Char = Player.Character

local Directions = {
	Idle = {Vector3.new(0, 0, 0), "Idle"};
	Go = {Vector3.new(0, 0, -1), "Go"};
	Back = {Vector3.new(0, 0, 1), "Back"};
	Right = {Vector3.new(1, 0, 0), "Right"};
	Left = {Vector3.new(-1, 0, 0), "Left"};
	AD_Right = {Vector3.new(1, 0, -1), "AD_Right"};
	AD_Left = {Vector3.new(-1, 0, -1), "AD_Left"};
	AA_Right = {Vector3.new(1, 0, 1), "AA_Right"};
	AA_Left = {Vector3.new(-1, 0, 1), "AA_Left"}
}

local Dist, Point = {}
for Index, Table in pairs(Directions) do
local PointX = #Dist+1
	
	Dist[Index] = 1E100
	delay(nil, function()
		while wait(.1) do
		Point = ((Char.PrimaryPart.Position+Char.PrimaryPart.Velocity.Unit)-(Char.PrimaryPart.Position+Char.PrimaryPart.CFrame:VectorToWorldSpace(Table[1]))).Magnitude
			
			Dist[PointX] = Point
				
			if Point == math.min(unpack(Dist)) and Point <= .5 then
				print(Table[2])
			end
		end
	end)
end
5 Likes

I’m pretty sure I can only use that if I use the MoveTo function

Nope, it works anytime you move iirc.

Your script is nice, but I tried printing character.PrimaryPart.Velocity.unit and I get this;

Vector3.new(-0.978591263, 0, 0.205813408)

Also if it works every time you move then why is it printing 0,0,0?

I tried printing WalkToPoint and it only prints 0,0,0 nothing else. So if there’s another way let me know

I’m about to head to bed as it’s almost 1 am. But this post should help:

Err still prints 0,0,0

	print(root.CFrame:VectorToObjectSpace())

This is a questions that’s kinda loaded, but that’s okay!

The main piece of information you’re missing and that you need to decide on is what you’re moving relative to.

For instance, say I wanted to know my character’s movement direction relative to my camera. When I hold the D key I’ll move lright of my camera. However, depending on which way my camera is facing in the world this may give me a very arbitrary Humanoid.MoveDirection so that’s not very useful information on its own.

Luckily for us we can solve this problem with the use of the dot product. We know the closer two unit vectors are to each other, the closer the value of the dot product is to 1. Thus, we can say something along the lines of:

local camera = game.Workspace.CurrentCamera
local humanoid = ... -- fill this in with your valid humanoid

if (humanoid.MoveDirection:Dot(camera.CFrame.RightVector) > 0.75) then
	-- we're going right
end

Okay, so that works well enough, but one problem we might face with this particular situation is that the camera is doing a top down view of the player. The MoveDirection is now not going to have a dot value with either camera.CFrame.LookVector (forward/backward) or camera.CFrame.RightVector (right/left) because due to how we tilted our camera the camera.CFrame.UpVector is actually the closest match. So in the case of trying to “localize” player movement direction I recommend you pull the object space move vector directly from the PlayerModule that every player loads with by default.

All that said, this dot product method may seem useless so far, but it’s actually in some other situations such as creating a boost pad or something.

if (boostPad.CFrame.LookVector:Dot(humanoid.MoveDirection) > 0.75) then
	-- boost!
end

Hope that helps!

69 Likes

This is actually working for me, however only in MouseLock Mode. In classic mode it only prints forwards, never prints backwards of the players moving backwards. I also want to be able to check if the player’s moving backwards so that I can play a specific animation for when they boost backwards.

EDIT: Actually it only prints forwards in classic mode never right, left, or backwards.

2 Likes

same issue, hopefully he would reply soon

1 Like

You should try using LookVector! or Raycasting!

Is there a way to check the angle instead of the number?

The way i do this is

if player is not shift locked / Mousebehaviour is set to locked Then it is always front
else

if w key then front
if s key then Back
if a key then Left
if d key then Right

that is how i do my dashes

js do it from the camera

you dont need to use the hrp