Detection of Player Movement Direction

Hello, I am making a simple dash script, where the player dashes towards whatever direction the player is moving. I have made a semi working system of detecting the direction the player is moving, but it doesn’t work for side to side. Any help would be appreciated!

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService('Players')
local InputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local dashAnimations = ReplicatedStorage:WaitForChild("DashAnimations")

local mainDashCooldown = false
local sideDashCooldown = false

local mainDashTime = 6
local sideDashTime = 2 

local char = script.Parent
local hrp = char:WaitForChild("HumanoidRootPart")
local humanoid = char:WaitForChild("Humanoid")

InputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	
	if input.KeyCode == Enum.KeyCode.Q and mainDashCooldown == false and sideDashCooldown == false then
		local direction = humanoid.MoveDirection:Dot(hrp.CFrame.LookVector)
		
		if tonumber(direction) >= 0.9 then
			print("Player is going forwards")
			
		elseif tonumber(direction) <= -0.9 then
			print("Player is going backwards")
		elseif tonumber(direction) == 0 then 
			print("Player is going sideways")
		end
	end
end)

3 Likes

Anyone help would be appreciated (bump)

2 Likes

What’s the point of calculating this part here? Do you need to specifically know which direction you are dashing in? Otherwise you might as well just apply the force(or whatever method you use) in the corresponding direction.

2 Likes

Yes, because I need to apply the force to the direction the player is walking. (The player will only have a direction they are walking in if they are in shift lock, otherwise they are always going forward.)

2 Likes

Yeah, but like are you unable to dash backwards or sideways or something? Because if you are allowed to dash in all 8 directions, then what’s the problem with just pushing the player that way?

2 Likes

Well, there are two separate cooldowns depending on if the player dashes backwards/forwards, or side to side. I could do your method, but I would still need to detect which way they are walking.

2 Likes
InputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.Q and not mainDashCooldown and not sideDashCooldown and humanoid.MoveDirection ~= Vector3.zero then
		local direction = math.round(humanoid.MoveDirection:Dot(hrp.CFrame.LookVector))

		if direction == 1 then
			print("Player is going forwards")
		elseif direction == -1 then
			print("Player is going backwards")
		else 
			print("Player is going sideways")
		end
	end
end)
2 Likes

How would I detect if they were going left or right?

2 Likes

another bump hahahaha gggggggg

2 Likes

if UIS:IsKeyDown(“D”) and UIS.MouseBehavior.EnumType == Enum.MouseBehavior.LockCenter then
print(“right”)

if UIS:IsKeyDown(“D”) and UIS.MouseBehavior.EnumType == Enum.MouseBehavior.LockCenter then
print(“left”)

im just wondering though, how would one go about diagional movement?

use HumanoidRootPart.CFrame.RightVector to detect sideways movement, -1 is left, 1 is right

local direction = math.round(Humanoid.MoveDirection:Dot(Hrp.CFrame.LookVector))
	local direction2 = math.round(Humanoid.MoveDirection:Dot(Hrp.CFrame.RightVector))

	if direction == 1 then
		print("Player is going forwards")
	elseif direction == -1 then
		print("Player is going backwards")
	elseif direction2 == 1 then
		print("Player is going right")
	elseif direction2 == -1 then
		print("Player is going left")
	end
1 Like

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