Disclaimer I have searched, I cannot find anything so far, so if there is something, please point me to it!
So I am trying to use humanoid.MoveDirection(), the only problem is that how I want to use it is to tell if the player is moving forwards, or backwards, or left to right.
So the problem is that MoveDirection returns a vector3, which when I first spawn in and start walking forwards its (0,0,-1) now this would work fine, but if you turn your character to face another direction, it changes, and is no longer (0,0,-1), instead it becomes quite wild, like -0.9332048892974854, 0, -0.3593446910381317, so how can I use moveDirection where it isnt based off of the direction the character is facing,
thanks~
~ Frodev
Try using the players move vector directly from the playerModule like this:
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local controls = require(localPlayer.PlayerScripts.PlayerModule):GetControls()
local inputVector = controls:GetMoveVector()
print(inputVector) --direction player is moving
I mean, it’s the most efficient way imo. I guess you could try maybe doing something like getting the difference between the players last position and new position then try and find the world space offset but like that’s pretty insane when the alternative is just sending remote events
a lot of cases, basically I want to be able to get the value any time I trigger a function, for instance I could do it just in general, which in that case yeah remove events would work, but I might need to do it every heartbeat, really I need to do this in like any context.
you can use the dot/cross product to determine the players move direction relative to the characters orientation like this:
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
local hum = script.Parent:WaitForChild("Humanoid")
while task.wait() do
local moveDir = hum.MoveDirection
local lookDir = hrp.CFrame.LookVector
local dot = moveDir:Dot(lookDir)
local cross = moveDir:Cross(lookDir)
if moveDir.Magnitude > 0 then
if dot > 0.1 then
print("Moving Forward")
elseif dot < -0.1 then
print("Moving Backward")
end
if cross.Y > 0.1 then
print("Moving Right")
elseif cross.Y < -0.1 then
print("Moving Left")
end
else
print("Not Moving")
end
end
this is just an example in a local script loop in StarterCharacterScripts but it should work in other contexts too
If it’s a character controller you would need to get the actual player input to apply the force into the desired direction so this might not work for what they want
True, in the specific case of a character controller you might need to use direct input to perform actions like applying force, im just giving a general solution assuming the default character movement.
Just realised i accidentally replied to you initialy instead of the poster sry
another easier way to find the relative move direction to the player can be like this:
local hrp = script.Parent:WaitForChild("HumanoidRootPart")
local hum = script.Parent:WaitForChild("Humanoid")
while task.wait() do
print(hrp.CFrame:VectorToObjectSpace(hum.MoveDirection))
end
this will always return
~ 0,0,-1 when walking forward,
~ 0,0,1 when walking backwards,
~ 1,0,0 when walking right,
~ -1,0,0 when walking left,