I’ve run into an issue with my game: the dash travels much further in the air than on the ground, and something seems to be pulling the player slightly backward.
I tried fixing it using LinearVelocity, but I must have done something wrong, as dashing in the air sometimes causes the player to fling a bit and fly off the ground.
Could someone help me do the correct way to fix this?
local function PerformDash(character, charData, dashType, dashDirection, state)
local humanoid = character:FindFirstChildOfClass("Humanoid")
local hrp = character:FindFirstChild("HumanoidRootPart")
if not humanoid or not hrp then return end
state.isDashing = true
local dashConfig = {
Speed = charData:Get({"Stats", dashType .. "Speed"}) or 75,
Duration = charData:Get({"Stats", dashType .. "Duration"}) or 0.25,
Smoothness = charData:Get({"Stats", dashType .. "Smoothness"}) or 75,
}
local duration = dashConfig.Duration
local elapsed = 0
local startSpeed = dashConfig.Speed
local endSpeed = dashConfig.Speed * 0.85
PlayDashAnimation(dashDirection)
if state.currentBodyVelocity then
state.currentBodyVelocity:Destroy()
end
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.MaxForce = Vector3.new(40000, 0, 40000)
bodyVelocity.P = 1250
bodyVelocity.Parent = hrp
state.currentBodyVelocity = bodyVelocity
local initialDir
if dashDirection == "Back" then
initialDir = -hrp.CFrame.LookVector
elseif dashDirection == "Right" then
initialDir = hrp.CFrame.RightVector
elseif dashDirection == "Left" then
initialDir = -hrp.CFrame.RightVector
end
initialDir = Vector3.new(initialDir.X, 0, initialDir.Z)
if initialDir.Magnitude > 0 then
initialDir = initialDir.Unit
end
local currentDir = initialDir
local connection
connection = RunService.Heartbeat:Connect(function(dt)
elapsed += dt
if elapsed >= duration then
connection:Disconnect()
if bodyVelocity then
bodyVelocity:Destroy()
state.currentBodyVelocity = nil
end
task.delay(0.5, function()
state.isDashing = false
end)
return
end
local targetDir
if dashDirection == "Back" then
targetDir = -hrp.CFrame.LookVector
elseif dashDirection == "Right" then
targetDir = hrp.CFrame.RightVector
elseif dashDirection == "Left" then
targetDir = -hrp.CFrame.RightVector
end
targetDir = Vector3.new(targetDir.X, 0, targetDir.Z)
if targetDir.Magnitude > 0 then
targetDir = targetDir.Unit
end
currentDir = currentDir:Lerp(targetDir, math.clamp(dt * dashConfig.Smoothness, 0, 1))
if currentDir.Magnitude > 0 then
currentDir = currentDir.Unit
end
local progress = elapsed / duration
local speedMultiplier = 1 - (progress ^ 2)
local currentSpeed = endSpeed + (startSpeed - endSpeed) * speedMultiplier
bodyVelocity.Velocity = currentDir * currentSpeed
end)
AddConnection(connection)
end