How to check if the player is walking backwards

The title explains itself, I want to check if the player is walking backwards (assuming they have shift-lock or first person enabled) so I can play the walking animation in reverse

1 Like

Check if they are pressing the S key, since you used assuming they are in shift lock the will always be facing forward so thiis method would work pretty well i guess

local LocalPlayer = game:GetService("Players").LocalPlayer
local Char = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid:Humanoid = Char:WaitForChild("Humanoid")

local HumanoidRootPart:BasePart = Char:WaitForChild("HumanoidRootPart")
-- Bare in mind when you are in a constant speed this doesn't get updated the "Humanoid.Running" Event
-- you could change this for a Runservice Instead.
Humanoid.Running:Connect(function(Speed)
	-- Compares where the humanoid is heading with movedirection and compares it to look vector of humanoidRootPart
	-- It should usually be -1  but with angles can depend so i've updated to :  -0.65 just
	if (HumanoidRootPart.CFrame.LookVector:Dot(Humanoid.MoveDirection)) <= -0.65 then
		-- Walking backwards
		print("Walking backwards")
	end	
end)



1 Like

I just tested your code but if the player has either A or D pressed before pressing S it doesn’t work

Yea that’s because the value is getting Updated with humanoid.Running Event therefore when you press a and d you are still moving in a constant speed. Try it with a while loop.

I haven’t tried this code yet, but see if it works:

if Humanoid.MoveDirection.Magnitude.Z < 0 then
       --> code
end

I just put it in a heartbeat loopp and it still doesnt work when i press A or D before

That wouldnt work because Magnitude is a number and it doesnt have any XYZ components that would return an error

image

Ah okay, thanks for letting me know.

1 Like

Reduce the number down to -0.6 - 0.7

1 Like

It worked! But could you explain why?

Since you are moving in a certain angle With A, D You have to go in between the numbers.
Dot product is used to compare if the objects are looking towards the same direction or away can be used for npc, ect in this case I’m comparing the HumanoidRootPart To where ever the humanoid is moving towards to know if it’s moving backwards or foward

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.