Creating a dash; BodyVelocity math or something else?

Oh I see the problem now, it’s being influenced by player inputs because the script also uses the humanoids MoveDirection, and since it updates every frame the velocity will always be affected by the MoveDirection

This should work now. Sorry for the misunderstandings.

local Camera = workspace.CurrentCamera

local Character = script.Parent

local Root = Character.HumanoidRootPart

local Humanoid = Character.Humanoid

local RunService = game:GetService("RunService")

local UserInputService = game:GetService("UserInputService")

local _DashCooldown = 0.5

local _DashDistance = 30

local _DashDuration = 0.5

local _DashSpeed = _DashDistance / _DashDuration

local LastDash = 0

local LastMDirection = Vector3.new()

--// Utilities

function EaseQuarticIn(a, b, t, d)

local c = b - a

t /= d

return c * t * t * t * t + a

end

function Lerp(a, b, t) -- Linear interpolation (if you prefer this)

return a + (b - a) * t

end

--// Functions

function OnRenderStepped()

-- Set character orientation

local _, CameraYOrientation, _ = Camera.CFrame:ToOrientation()

Root.CFrame = CFrame.new(Root.Position) * CFrame.fromOrientation(0, CameraYOrientation, 0)

-- Calculate speed

local DashSpeed = EaseQuarticIn(_DashSpeed, 0, math.min(tick() - LastDash, _DashDuration), _DashDuration) -- Decelerate towards 0 speed

-- Apply velocity

local DashVelocity = LastMDirection * DashSpeed

local Velocity = Root.AssemblyLinearVelocity

Root.AssemblyLinearVelocity = DashSpeed > 0 and Vector3.new(DashVelocity.X, Velocity.Y, DashVelocity.Z) or Velocity

end

function Dash()

local t = tick()

if t - LastDash >= _DashCooldown and Humanoid.MoveDirection ~= Vector3.new() then -- Won't dash if standing still

LastDash = t

LastMDirection = Humanoid.MoveDirection

end

end

function OnInputBegan(Input, Processed)

if Processed then return end

if Input.KeyCode == Enum.KeyCode.LeftShift then

Dash()

end

end

--// Events

RunService.RenderStepped:Connect(OnRenderStepped)

UserInputService.InputBegan:Connect(OnInputBegan)
3 Likes

I think you finally did it man! let me put the animation in I am so happy to have you!

1 Like