Dash goes way longer in air

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

Friction is stopping you when on the ground

2 Likes

That doesn’t help all that much, the problem still remains

You could fix this by adding a very subtle upwards force to the body velocity to counteract the frictional force.

An alternative would be to reduce the bodyVelociy’s force if the players humanoid is in a HumanoidStateType is FallingDown, Flying, FreeFall, Ragdoll, or Jumping. However, this would be more tedious as if the player hits the ground mid dash the reduced force and friction would stack.

Easiest way to balance them is by slowing down airspeed

if humanoid.FloorMaterial == Enum.Material.Air then
    currentSpeed *= 0.75
end

Check out this thread for a very similar issue

Will this fix the issue where the player also gets pulled backwards slightly?

it’s called friction, intentional feature of the roblox engine

1 Like

how is this related to the fact that the player is literally also being pulled back?

BodyVelocity is deprecated; use VectorVelocity or LinearVelocity. Additionally, try using ApplyImpulse.

When destroying the BodyVelocity, set the HumanoidRootPart’s AssemblyLinearVelocity to Vector3.zero. This should cancel any monentum from the dash

bodyVelocity:Destroy()
hrp.AssemblyLinearVelocity = Vector3.zero

i solved this problem by reducing the max force, allowing the ground speed to reach its maximum without being limited by friction

1 Like

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