How to check if player is moving backwards? i am making a first person game i don’t want the player running when he is pressing S.
If humanoid.MoveDirection.Z > 0
then you know they are moving backwards. -1
would be forward, 1
would be backwards.
You can also use the sign of the dot product between the velocity of the player character’s HRP and the look-vector of the HRP’s CFrame. If it is positive then they are moving forward, negative if they are moving backwards, and zero if sideways.
is number 1 forward or backwards?
1 is backward and -1 is forward
but why is the d key affected?
This only works if the character only faces the default direction in the world; it assumes the player doesn’t turn around. The MoveDirection vector property is not relative to the character’s orientation. However, the direction can be manipulated to set relative to the player’s camera. For example, this code can be used to see if the player is moving towards or away from the camera. This method works best if you have your camera locked in one direction (like Z-Targetting an enemy).
local forwardBackDir = hum.MoveDirection:Dot(cam.CFrame.LookVector)
if forwardBackDir < -0.75 then --Backwards
--Stuff
elseif forwardBackDir > 0.75 then --Forwards
--Other Stuff
end
You can also alter this to detect if the player is going right or left relative to the camera using the camera’s right vector instead of look vector.
Alternatively, you can use the character’s velocity to determine if they are moving backwards or forwards.
--Get the HRP's horizontal velocity
local torVel = (hrp.Velocity * v3(1, 0, 1)).magnitude
--Checks if the HRP has velocity above a certain threshold, and if so, put the values in a vector2
local directionVelocity = (v3(hrp.Velocity.X, hrp.Velocity.Z).magnitude > 0) and Vector2.new(hrp.Velocity.X, hrp.Velocity.Z).unit or Vector2.new(0, 0)
--Creates a vector2 unit value for direction
local dir = Vector2.new(hrp.CFrame.lookVector.X, hrp.CFrame.lookVector.Z).unit
--Simplifies direction unit vector and direction velocity into a number for forwards/backwards
local dirNum = math.floor((directionVelocity - dir).magnitude + 0.5)
An example usage of these values can be for determining whether the humanoid root part (HRP) has forward, backwards, or idle velocity:
if torVel > 1 then
stateVal.Value = dirNum < 2 and "Forward" or "Backward" --Shorthand if statement
else
stateVal.Value = "Idle"
end
This approach doesn’t require the the camera, which can be useful if you want a free moving camera instead of a locked one. However, this method will throw some false positives if the character is given a velocity over your thresholds (for example, if the player is on sliding platforms).
Perhaps a combination of this and usage of the MoveDirection property can create in a more accurate results, but I haven’t come across a need for that yet. Hope this points you in the right direction.
There is a better version to find out the velocity and it is updated to the new velocity.
local character = script.Parent
while true do
task.wait(0.1)
local direction = character:GetPivot():VectorToObjectSpace(character.HumanoidRootPart.AssemblyLinearVelocity)
print(string.format("%.2f, %.2f, %.2f", direction.X, direction.Y, direction.Z))
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.