How can I ensure the velocity slows down consistently across different frame rates?

So I have this BodyVelocity that slows down every frame, but the issue is if you’re laggy or have more than 60 Fps it will either be too fast or too slow. Here is the code

local BV = Instance.new("BodyVelocity", Character.HumanoidRootPart) 
BV.Velocity = Character.HumanoidRootPart.CFrame.LookVector * Power
BV.MaxForce = Vector3.new(9e9, 0, 9e9)
game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
	Power *= 0.9
	BV.Velocity = Character.HumanoidRootPart.CFrame.LookVector * Power
end)

You’ll need to incorpoate the deltaTime value into it, so that the value of power changes at different rates depending on the framerate.

I’m not sure the exact formula you would need to use, but it’s going to be something based on this:

Power *= 0.9 * (deltaTime/60)
1 Like

That’s actually what deltaTime is used to do! Also,

Don’t connect/bind functions to the render step unless absolutely necessary.

I remembered reading this in the docs, so I went and found it. The same page also says

gameplay logic that affects the physics state should be done in PreSimulation, such as setting the Velocity of parts. (Source)

So, (1), your code should incorporate deltaTime, and (2), replace RenderStepped with PreSimulation. That could look something like:

local BV = Instance.new("BodyVelocity", Character.HumanoidRootPart) 
BV.Velocity = Character.HumanoidRootPart.CFrame.LookVector * Power
BV.MaxForce = Vector3.new(9e9, 0, 9e9)

game:GetService("RunService").PreSimulation:Connect(function(deltaTime)
	Power *= 0.9^(deltaTime * 60)
	BV.Velocity = Character.HumanoidRootPart.CFrame.LookVector * Power
end)

You could double-check my logic here: Power *= 0.9^(deltaTime * 60).

Not sure this would work. OP seems to want this change every 1/60th of a frame. You’d want deltaTime * 60, so then 1/60th of a second would equate to 1, and so on.

Plus, I think the correct form is Power *= 0.9^(...), even though it looks weird. For example, after 2/60ths of a sec, you’d have Power *= 0.9^(2) = 0.81, which I think is the right factor to multiply by.

1 Like