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.
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.
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
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
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.