My goal is to make function that detects in what direction player wants to move without magnitude and other things bc sometimes character move in one direction but he wants in opposite.
I tried ContextActionService but its not work for mobile somewhy.
local ContextActionService = game:GetService("ContextActionService")
local ActionName = "WalkingForward"
local function HandleAction(actionName, inputState, inputObject)
if actionName == ActionName and inputState == Enum.UserInputState.Begin then
print("WALKINGFORWARD")
end
end
ContextActionService:BindActionAtPriority(ActionName,HandleAction,false,200,Enum.PlayerActions.CharacterBackward)
The function i need to need to detect where he wants to move, not depend on character physics and for any device.
local runService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera
function GetPlayerLocalDirection()
if Character:FindFirstChildOfClass("Humanoid") then
local MoveDirection = Character:FindFirstChildOfClass("Humanoid").MoveDirection
local cameraLook = Camera.CFrame.LookVector
local cameraLookXZ = Vector3.new(cameraLook.X, 0, cameraLook.Z).Unit
local cameraRight = cameraLookXZ:Cross(Vector3.new(0, 1, 0))
local localX = MoveDirection:Dot(cameraRight)
local localZ = MoveDirection:Dot(cameraLookXZ)
local roundedX = math.floor(localX + 0.5)
local roundedZ = math.floor(localZ + 0.5)
return roundedX, roundedZ
end
return 0, 0
end
function PrintDirection()
local X, Z = GetPlayerLocalDirection()
if X > 0 then
print("Moved Right")
elseif X < 0 then
print("Moved Left")
end
if Z > 0 then
print("Moved Forward")
elseif Z < 0 then
print("Moved Backward")
end
end
runService.RenderStepped:Connect(PrintDirection)
There’s a much, much simpler method and that’s using the built-in methods provided by PlayerScripts:
-->> in a local script
local Players = game:GetService("Players")
local PlayerModule = require(Players.LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()
game["Run Service"].Heartbeat:Connect(function()
local moveDirection = Controls:GetMoveVector()
print(moveDirection) --> prints the direction the player is moving
if moveDirection == Vector3.new(0, 0, -1) then
print("Forward")
end
end)
I’m assuming you want to find out if the player is moving generally forward. If the player moves forward slightly to the right, it would still count as forward.
This can be easily implemented using the :Dot() operator.
local moveDirection = Controls:GetMoveVector()
if moveDirection:Dot(Vector3.new(0, 0, -1)) >= 0.5 then --> if the player is moving 45 degrees within the forward direction this will pass
print("Forward")
end
So you can check if the player is moving forward with this:
local Players = game:GetService("Players")
local PlayerModule = require(Players.LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()
local function isMovingForward()
local moveDirection = Controls:GetMoveVector()
return moveDirection:Dot(Vector3.new(0, 0, -1)) >= 0.5
end