Checking Which Direction A Player Moves

I’m trying to figure out a way to check which direction the player is moving.

I’m thinking of using humanoid.MoveDirection to check if the player is moving a certain direction to make them boost that direction when you press a key.

My problem is that I don’t know how to check which direction the player’s moving, if it’s left or right etc.

17 Likes

I’ve tried

if humanoid.MoveDirection ~= Vector3.new(0,0,0) then

end

to check if the player’s moving or not, and that works I just want to know which direction theyre moving.

You can check the MoveDirection.X and MoveDirection.Z and go from there.

I think you can also use toObjectSpace and toWorldSpace on MoveDirection itself if you’re wanting which way they are moving from a certain object/point.

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!

73 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!