Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I detect if a player is moving backwards?

I'm making an FPS and you can sprint backwards which is a problem so I'm trying to make it so you can't sprint backwards, how would I do this?

I tried using cframes and lookvectors but it doesn't seem to work as none of the axis' return what i need.

local movedir = CFrame.new(last_p,cur_p).lookVector-hrp.CFrame.lookVector
print(movedir.unit.Y)

X just returns random gibberish when you are moving Y seems to return something that has to do with the Y velocity not the Y axis in rotation Z also just seems to return random gibberish when you are moving

Any help?

1 answer

✓ Accepted answer

You want to use the dot product.

The dot product basically measures "how much" one vector is in the direction of another.

If a and b are both unit vectors, then a:Dot(b) == 1 means they point in the same direction; a:Dot(b) == 0 means they're perpendicular, and a:Dot(b) == -1 means they're opposite.

local forward = workspace.CurrentCamera.CFrame.lookVector

local playerDirection = torso.Velocity

if forward:Dot(playerDirection) > 0 then
	-- forward!
else
	-- backward!
end

1 comment

0

I never knew that dot was a thing, thanks for informing me!

This discussion is preserved as it was. New answers, comments, and votes are closed.