Faster way to detect Moving Direction?

so I’m making a soccer game, and for the Dribble mechanics , the dribble direction depends on the moving direction of the player, so for example if I hit the Dribble key and move to the left side , the ball will move to the left side

the issue with my script is that sometimes it takes time to update the moving direction , so if i want to dribble to the left side (by Dribbling and moving the the left side) i need to hold the move key for a moment and dribble for it to work, it i try to do it really fast , it will not give the desired dribble (most of time the ball will go forward like i didnt move at all)

there is a game that have similar dribble mechanics , its called Kickoff club Alpha and they have better way to detect the moving detection , i don’t think they are using WASD for it cuz they have a mobile version

This is the script im using to get the player moving direction, the script also works for diagonal movement:

function SCModule.GetMovingDirection()
	local cos30=math.cos(math.pi/6)
	local cos60=math.cos(math.pi/3)
	local char = game.Players.LocalPlayer.Character
	local walkDir=char.HumanoidRootPart.CFrame.Rotation:PointToObjectSpace(Vector3.new(char.HumanoidRootPart.Velocity.X,0,char.HumanoidRootPart.Velocity.Z).Unit)
	if walkDir.X>cos60 and walkDir.X<cos30 then
		if walkDir.Z<0 then
			return "forward-right"
		else
			return "backward-right"
		end
	elseif walkDir.X<-cos60 and walkDir.X>-cos30 then
		if walkDir.Z<0 then
			return "forward-left"
		else
			return "backward-left"
		end
	elseif walkDir.X>-cos60 and walkDir.X<cos60 then
		if walkDir.Z<0 then
			return "forward"
		else
			return "backward"
		end
	elseif walkDir.Z>-cos60 and walkDir.Z<cos60 then
		if walkDir.X<0 then
			return "left"
		else
			return "right"
		end
	else
		return "forward"
	end
end

Appreciate any help thanks.

1 Like

I am not 100% sure whether it’s faster than your current method or more efficient, but have you tried using the Move direction property of the humanoid?

It’s a property of the humanoid that gives you the unit vector of the current direction the humanoid is walking to.

1 Like
local DirectionOfMovement = HumanoidRootPart.CFrame:VectorToObjectSpace( HumanoidRootPart.AssemblyLinearVelocity )

local Forward = math.abs( math.clamp( DirectionOfMovement.Z / Humanoid.WalkSpeed, -1, -0.01 ) )
local Backwards = math.abs( math.clamp( DirectionOfMovement.Z / Humanoid.WalkSpeed, 0.01, 1 ) )
local Right = math.abs( math.clamp( DirectionOfMovement.X / Humanoid.WalkSpeed, 0.01, 1 ) )
local Left = math.abs( math.clamp( DirectionOfMovement.X / Humanoid.WalkSpeed, -1, -0.01 ) )

in a renderstepped should work

4 Likes

I actually need a script that work with diagonal movement

1 Like

It does, for example moving backwards and left will increase both backwards and left velocity values

1 Like

ok so i found a better way to get movement direction, i used GetMoveVector from controlmodule
and it update faster than the old function, here is script if anyone need :

local ControlModule = require(player:WaitForChild("PlayerScripts").PlayerModule:WaitForChild("ControlModule"))

local MoveVector = ControlModule:GetMoveVector()
	if MoveVector.Z < 0 and MoveVector.X > 0 then
		return "forward-right"
	elseif MoveVector.Z < 0 and MoveVector.X < 0 then
		return "forward-left"
	elseif MoveVector.Z > 0 and MoveVector.X > 0 then
		return "backward-right"
	elseif MoveVector.Z > 0 and MoveVector.X < 0 then
		return "backward-left"	
	elseif MoveVector.Z == 0 and MoveVector.X > 0 then
		return "right"
	elseif MoveVector.Z == 0 and MoveVector.X < 0 then
		return "left"
	elseif MoveVector.Z > 0 and MoveVector.X == 0 then
		return "backward"
	elseif MoveVector.Z < 0 and MoveVector.X == 0 then
	    return "forward"
	else
		return "forward"
	end
2 Likes

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