Smooth Velocity?

I want to the movement of the dash to be smooth but it lags behind

I have tried loops and even hearbeat. I’ve seen other games have smooth dash system.

How do they achieve this?

my code:

remote.OnServerEvent:Connect(function(Player, objective)
	
	local char = Player.Character
	local humRP = char.HumanoidRootPart
	
	if objective == "Forward" then
		
		Player.Data.Stamina.Value = Player.Data.Stamina.Value - 15
		
		local sound = script.DashSound:Clone()
		sound.Parent = humRP
		sound.EmitterSize = 15
		sound:Play()
		game.Debris:AddItem(sound,1)
		
		local Velocity = 60 - Player.Weight.Value * 5
		if Velocity <0 then
			Velocity = 0
		end
		
		char.Humanoid:LoadAnimation(script.DashForward):Play()
		
		local BV = Instance.new("BodyVelocity", humRP)
		BV.Name = "DashPosition"
		BV.MaxForce = Vector3.new(15000,0, 15000)
		
		spawn(function()
			while BV do
				BV.Velocity = humRP.CFrame.LookVector*Velocity
				wait()
			end
		end)	
		
		wait(0.7)
		BV:Destroy()
           end)

use RunService.RenderStepped, which fires every frame, and TweenService to smoothly interpolate the velocity change. Here’s an updated version of your code:

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

remote.OnServerEvent:Connect(function(Player, objective)
	
	local char = Player.Character
	local humRP = char.HumanoidRootPart
	
	if objective == "Forward" then
		
		Player.Data.Stamina.Value = Player.Data.Stamina.Value - 15
		
		local sound = script.DashSound:Clone()
		sound.Parent = humRP
		sound.EmitterSize = 15
		sound:Play()
		game.Debris:AddItem(sound,1)
		
		local Velocity = 60 - Player.Weight.Value * 5
		if Velocity < 0 then
			Velocity = 0
		end
		
		char.Humanoid:LoadAnimation(script.DashForward):Play()
		
		local BV = Instance.new("BodyVelocity", humRP)
		BV.Name = "DashPosition"
		BV.MaxForce = Vector3.new(15000,0, 15000)
		
		local tweenInfo = TweenInfo.new(0.7, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
		local tween = TweenService:Create(BV, tweenInfo, {Velocity = humRP.CFrame.LookVector * Velocity})
		tween:Play()
		
		local connection
		connection = RunService.RenderStepped:Connect(function()
			if not BV then
				connection:Disconnect()
			else
				BV.Velocity = humRP.CFrame.LookVector * Velocity
			end
		end)
		
		wait(0.7)
		BV:Destroy()
		connection:Disconnect()
	end
end)

This code uses RunService.RenderStepped to update the velocity every frame and TweenService to smoothly interpolate the velocity change.

I Replaced the spawn function with a connection to RunService.RenderStepped:
RunService.RenderStepped fires every frame, providing more consistent and smoother updates to the character’s velocity during the dash.

local connection
connection = RunService.RenderStepped:Connect(function()
	if not BV then
		connection:Disconnect()
	else
		BV.Velocity = humRP.CFrame.LookVector * Velocity
	end
end)

I also added TweenService to interpolate the velocity change:
By using TweenService, the velocity change will be smoothly interpolated over the specified duration (0.7 seconds in this case). This will make the dash movement feel smoother and more natural.

local tweenInfo = TweenInfo.new(0.7, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local tween = TweenService:Create(BV, tweenInfo, {Velocity = humRP.CFrame.LookVector * Velocity})
tween:Play()

Lastly I replaced the wait(0.7) with a tween.Completed:Wait():
This change will ensure that the BodyVelocity is destroyed and the connection is disconnected only after the tween has completed.

tween.Completed:Wait()
BV:Destroy()
connection:Disconnect()
3 Likes