How do I detect if a player is moving 'forwards' without using input keys and direction relative to camera?

I’ve been stumped with this for a while and all the solutions I’ve come across don’t work the way I need it to and/or are unreliable.
Is there any specific way to detect if a player is moving forward without:

  1. Using direction relative to camera (Must be free-camera friendly)
  2. Detecting what key the player is pressing (Must be platform friendly)
  3. Without using MoveDirection (Only assumes the player is facing the default direction)

Any help would be greatly appreciated.

There are different approaches to detecting if a player is moving forward without using input keys or direction relative to camera. One possible solution is to use the player’s velocity to determine their movement direction.

Example;

-- Define a reference vector pointing forward in world space
local forwardVector = Vector3.new(0, 0, -1)

-- Define a threshold value for the minimum velocity magnitude
local minVelocityMagnitude = 0.1

-- Check if the player's velocity is mostly in the forward direction
local function isMovingForward(player)
    local velocity = player.Character.HumanoidRootPart.Velocity
    local velocityDirection = velocity.unit
    local dotProduct = forwardVector:Dot(velocityDirection)
    return dotProduct >= 0.8 and velocity.magnitude >= minVelocityMagnitude
end

-- Example usage:
local player = game.Players.LocalPlayer
if isMovingForward(player) then
    print("Player is moving forward")
else
    print("Player is not moving forward")
end

This implementation checks if the player’s velocity vector is mostly aligned with the forward direction vector in world space (dot product >= 0.8) and has a minimum magnitude (0.1 in this case). You can adjust these threshold values to suit your needs.

Note that this approach assumes that the player’s movement is purely translational and does not involve rotation. If the player is moving in a circular path or rotating while moving forward, this method may not work as expected.

This is not the solution I’m looking for; as stated, I want to be able to detect if the player is actually moving forward, and this uses the world’s default ‘forward’ direction. To clarify, I need to check if the player is moving forward in the direction they are facing. Thanks for your response anyways!

Then just change the forwardVector to HumanoidRootPart.CFrame.LookVector.

this might help: Best way to check if the player is moving & what direction they are moving?

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