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)
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.
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.)
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?
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.
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)
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