How to check if a players moving forwards without keys?

I know you can use MoveDirection to determine if the players moving in general. But what I want to know is how to tell if a players just moving forwards instead of backwards?

1 Like

If the Z axis of the MoveDirection is negative, then the player is moving forward.

I believe you are looking for whether the player is moving forward relative to how their character is facing so you can decide if they are strafing left, right or moving forwards or backwards.

You can do this by doing some CFrame magic to make the movement vector-like @sjr04 suggested relative to where the HumanoidRootPart is facing

	local movementVector = humanoidRootPart.Velocity*normalize*tiltFactor
	movementVector = (humanoidRootPart.CFrame-humanoidRootPart.CFrame.p):Inverse()*movementVector

This makes it if you are moving forward relative to where the character is facing the movement vector will be:

(0,0,-1)*VelocityMagnitude

Consequently, you can do some cool things like this local tilt script.

Script
local localPlayer = game:GetService("Players").LocalPlayer
local Character = localPlayer.Character or localPlayer.CharacterAdded:wait()
local lowerTorso = Character:WaitForChild("LowerTorso")
local humanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local normalize = Vector3.new(1,0,1)

local c1Store = lowerTorso.Root.C1

local tiltFactor = 2.5

game:GetService("RunService").RenderStepped:Connect(function()
	local movementVector = humanoidRootPart.Velocity*normalize*tiltFactor
	movementVector = (humanoidRootPart.CFrame-humanoidRootPart.CFrame.p):Inverse()*movementVector
	local x = math.rad(movementVector.X)
	local z = math.rad(movementVector.Z)
	
	local goalCFrame = CFrame.Angles(-z,0,x)
	lowerTorso.Root.C1 = lowerTorso.Root.C1:Lerp(goalCFrame,0.25)
end)
4 Likes

this script, plus my fly script, just gets cursed

1 Like

Sorry for bumping but what about backwards? (I never use movedirection)

If Z is negative while moving forwards then Z should be positive while moving backward.

You can investigate this yourself by printing this property in the studio. Like in this local script that I gave above which you can put in starter player scripts:

local localPlayer = game:GetService("Players").LocalPlayer
local Character = localPlayer.Character or localPlayer.CharacterAdded:wait()
local lowerTorso = Character:WaitForChild("LowerTorso")
local humanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local normalize = Vector3.new(1,0,1)

local c1Store = lowerTorso.Root.C1

local tiltFactor = 2.5

game:GetService("RunService").RenderStepped:Connect(function()
	local movementVector = humanoidRootPart.Velocity*normalize*tiltFactor
	movementVector = (humanoidRootPart.CFrame-humanoidRootPart.CFrame.p):Inverse()*movementVector
	local x = math.rad(movementVector.X)
	local z = math.rad(movementVector.Z)
print(x,z)
end)

Just move around and see what happens the x,z numbers and how it becomes positive and negative.

2 Likes

Sorry for bumping this topic but I am not sure how to convert that script to be R6 compatible as I tried and it broke the character.

just change lower torso to torso