How to detect if the player is walking forwards or backwards?

So i’m trying to make a Dash System that :

→ Makes you dash forwards if you walk towards the front
→ Makes you dash backwards if you walk backwards


But the problem is that I don’t know how I can detect if the player goes frontwards or backwards.
Any help?

1 Like

You can utilize Humanoid.MoveDirection.

If you want to be a bit more precise, you could check the players velocity.

1 Like

So like this?

if humanoid.MoveDirection.Magnitude > 0 then
   DashForwards()
end
if humanoid.MoveDirection.Magnitude < 0 then
   DashBackwards()
end

You can use elseif instead of multiple if statements and the Magnitude of a vector3 can never be 0 so your dash backwards function would never fire

If you used the logic to find magnitude then you’d realise magnitude can never be less than 0. :rofl:

2 Likes

You can use cframe direction rotation to get what your looking for

local char: Model = script.Parent
local Hum: Humanoid = char:WaitForChild("Humanoid")
local HumRP = char.PrimaryPart

Hum.Running:Connect(function()
	local directive = HumRP.CFrame:VectorToObjectSpace(Hum.MoveDirection)
	
	if directive.Z < 0 then
		print("Going forward")
	elseif directive.Z > 0 then
		print("Going Backward")
	end
end)

Edit: this script was placed in startercharacterscripts

10 Likes

I thought directive<0 is backward can u explain that thing??

By Default, -Z is forward direction of an object, and when we transform the MoveDirection relative to the world to the humanoidrootpart we check if it’s moving z is positive(backwards) or negative(forwards)

2 Likes

I thout z is forward thanks for the info

I think you can correct it like this??

if humanoid.MoveDirection.Z < 0 then
   DashForwards()
end
if humanoid.MoveDirection.Z > 0 then
   DashBackwards()
end
1 Like

This worked perfectly. Thanks!

Note that you can do the same thing, but a little bit shorter with:

local movement = HumRP.CFrame.LookVector:Dot(Hum.MoveDirection)

This will get you just directive.Z without .Y or .X.
movement will be positive when moving forward, zero when standing still or moving sideways and negative when moving backward.

@KrxKenDev
Speaking of moving sideways, what if the player is moving sideways and dashes?
Should he dash forward or backward?
Clearly, the player should dash to the side.
Therefore, you should simply dash in the current movement direction instead of trying to determine whether the player is going forward or backward.
The movement direction is humanoid.MoveDirection or humanoidrootpart.Velocity * Vector3.new(1, 0, 1).
If either of these vectors have a very small magnitude (player is standing still), then dash forward instead.

(Do not mark my post as solved, the post I’m replying to actually answers the question, I’m just saying the question should’ve been different)

7 Likes

what about right and left? this would help a lot please

Check for the x coordinates, if directive.X is positive then player is going right and if directive.X is negative then player is going left

it actually didnt work i even implemented the same system you used

It works fine for me, you might want to start a new topic about it however as to not bump this old one