function Dash.HandleDashDuration()
local startTime = tick()
local startFov = camera.FieldOfView
local goalFov = defaultFOV + DashInfo.fovIncrease
local lastTick = tick()
while tick() - startTime < DashInfo.duration do
local deltaTime = tick() - lastTick
lastTick = tick()
if camera.FieldOfView < goalFov then
local newFov = Dash.Lerp(camera.FieldOfView, goalFov, deltaTime * 20)
camera.FieldOfView = newFov
end
local lookDirection = camera.CFrame.LookVector * Vector3.new(1, 0, 1)
if lookDirection.Magnitude > 0 then
local planeDirection = Vector2.new(lookDirection.X, lookDirection.Z).Unit
linearVelocity.PlaneVelocity = planeDirection * DashInfo.speed
root.CFrame = CFrame.new(root.Position, root.Position + lookDirection * 1000)
end
if Dash.IsDashBlocked() then
break
end
task.wait()
end
end
--// Input Handling
UserInputService.InputBegan:Connect(function(input, gpe)
if gpe then return end
local key = input.KeyCode
if key == Enum.KeyCode.W then heldKeys.w = true
elseif key == Enum.KeyCode.S then heldKeys.s = true
elseif key == Enum.KeyCode.A then heldKeys.a = true
elseif key == Enum.KeyCode.D then heldKeys.d = true
end
end)
UserInputService.InputEnded:Connect(function(input, gpe)
if gpe then return end
local key = input.KeyCode
if key == Enum.KeyCode.W then heldKeys.w = false
elseif key == Enum.KeyCode.S then heldKeys.s = false
elseif key == Enum.KeyCode.A then heldKeys.a = false
elseif key == Enum.KeyCode.D then heldKeys.d = false
end
end)
it doesn’t look like youre ever actually checking if these keys are held in the dash script? i would also recommend using UserInputService:IsKeyDown() instead
So what is the end goal you are looking for with this dash. Would you also like it to be to diagonal as in having multiple directions if the player were to be holding down two keys??
local listedKeys = {
Enum.KeyCode.W;
Enum.KeyCode.A;
Enum.KeyCode.S;
Enum.KeyCode.D;
}
local heldKey = {
['wKey'] = 0;
['aKey'] = 0;
['sKey'] = 0;
['dKey'] = 0;
}
--- will be using binary values as booleans you could convert back with this function:
function binaryBool(given)
local value = math.round( math.abs(math.sign(given) ) ) ---[[[ allows any numerical value to become binary ]]]
if value == 0 then
return false
else if value == 1 then
return true
end
end
UserInputService.InputBegan:Connect(function(input, gpe)
if gpe then return end
local key = input.KeyCode
if table.find(listedKeys, key) then
heldKey[toString(key.Name)...'key'] = 1
end
end)
UserInputService.InputEnded:Connect(function(input, gpe)
if gpe then return end
local key = input.KeyCode
if table.find(listedKeys, key) then
heldKey[toString(key.Name)...'key'] = 0
end
end)
With this it is simpler for you to add the values with each other and also quickly change the direction of the character’s dash.
However, I know this doesn’t solve the underlying problem but it makes it simpler to understand your end goal with how you want the dash to function.
--// Services
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
--// Dash Config
local DashInfo = {
cooldown = 1.5,
speed = 50,
duration = 0.3,
range = 8,
keys = {Enum.KeyCode.Q},
actionName = "Dash_Action"
}
--// Variables
local onCooldown = false
local heldKeys = {w = false, a = false, s = false, d = false}
local camera = workspace.CurrentCamera
local char = script.Parent
local root = char:WaitForChild("HumanoidRootPart")
local hum = char:WaitForChild("Humanoid")
--// Linear Velocity Setup
local attachment = Instance.new("Attachment", root)
attachment.Name = "DashAttachment"
local linearVelocity = Instance.new("LinearVelocity", root)
linearVelocity.Attachment0 = attachment
linearVelocity.VelocityConstraintMode = Enum.VelocityConstraintMode.Plane
linearVelocity.PrimaryTangentAxis = Vector3.new(1, 0, 0)
linearVelocity.SecondaryTangentAxis = Vector3.new(0, 0, 1)
linearVelocity.MaxForce = math.huge
linearVelocity.Enabled = false
--// Animations
local Animations = script
local dashAnims = {
Front = hum:LoadAnimation(Animations:WaitForChild("Front")),
Left = hum:LoadAnimation(Animations:WaitForChild("Left")),
Right = hum:LoadAnimation(Animations:WaitForChild("Right")),
Back = hum:LoadAnimation(Animations:WaitForChild("Back"))
}
--// Raycast
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
--// Dash Logic
local function executeDash(actionName, inputState)
if onCooldown or actionName ~= DashInfo.actionName or inputState ~= Enum.UserInputState.Begin then return end
onCooldown = true
for _, anim in pairs(dashAnims) do anim:Stop() end
local anim
if UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter and not (heldKeys.w or heldKeys.a or heldKeys.d or heldKeys.s) then
anim = dashAnims.Front
elseif heldKeys.w then
anim = dashAnims.Front
elseif heldKeys.a then
anim = dashAnims.Left
elseif heldKeys.d then
anim = dashAnims.Right
elseif heldKeys.s then
anim = dashAnims.Back
else
anim = dashAnims.Front
end
anim:Play()
local vectorMask = Vector3.new(1, 0, 1)
local direction = root.AssemblyLinearVelocity * vectorMask
if direction.Magnitude <= 0.1 then
direction = camera.CFrame.LookVector * vectorMask
end
local dashVelocity = Vector2.new(direction.X, direction.Z).Unit * DashInfo.speed
linearVelocity.PlaneVelocity = dashVelocity
linearVelocity.Enabled = true
local startTime = time()
while time() - startTime < DashInfo.duration do
local lookDirection = camera.CFrame.LookVector * vectorMask
if lookDirection.Magnitude > 0 then
linearVelocity.PlaneVelocity = Vector2.new(lookDirection.X, lookDirection.Z).Unit * DashInfo.speed
root.CFrame = CFrame.new(root.Position, root.Position + lookDirection * 1000)
end
local ray = workspace:Raycast(root.Position, root.CFrame.LookVector * DashInfo.range, raycastParams)
if ray and ray.Instance.CanCollide and ray.Instance.Anchored then break end
task.wait()
end
linearVelocity.Enabled = false
task.wait(DashInfo.cooldown - DashInfo.duration)
onCooldown = false
end
--// Bind Actions
ContextActionService:BindAction(DashInfo.actionName, executeDash, true, unpack(DashInfo.keys))
--// Input Handling
UserInputService.InputBegan:Connect(function(input, gpe)
if gpe then return end
local key = input.KeyCode
if key == Enum.KeyCode.W then
heldKeys.w = true
elseif key == Enum.KeyCode.S then
heldKeys.s = true
elseif key == Enum.KeyCode.A then
heldKeys.a = true
elseif key == Enum.KeyCode.D then
heldKeys.d = true
end
end)
UserInputService.InputEnded:Connect(function(input, gpe)
if gpe then return end
local key = input.KeyCode
if key == Enum.KeyCode.W then
heldKeys.w = false
elseif key == Enum.KeyCode.S then
heldKeys.s = false
elseif key == Enum.KeyCode.A then
heldKeys.a = false
elseif key == Enum.KeyCode.D then
heldKeys.d = false
end
end)