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