Rate limiting DeltaTime for gun sway on high FPS players

Hi, I’m having an issue with trying to limit DeltaTime so that players with high FPS dont have really weird weapon sway. The way I’m trying to achieve this right now is with a rate limiter

local Rate = 0.5
local Accumulated = 0
local function onUpdate(dt)
	if bequipped == true then
		local Delta = game.UserInputService:GetMouseDelta()
		-- Shoving
		SwaySpring:shove(Vector3.new(-Delta.X/500, Delta.Y/500, 0))
		BobSpring:shove(Vector3.new(Bob(5), Bob(10), Bob(5)) / 10 * (Character.PrimaryPart.Velocity.Magnitude) / 10)
		-- Updating springs
		local UpdatedSway = SwaySpring:update(dt)
		local UpdatedBob = BobSpring:update(dt)
		-- Applying springs
		Accumulated += dt
		while Accumulated >= Rate do
			Accumulated -= Rate
			viewModel:PivotTo(camera.CFrame * CFrame.new(UpdatedSway.X, UpdatedSway.Y, 0) * CFrame.new(UpdatedBob.X, UpdatedBob.Y, 0))
		end
	end
end

This script is supposedly working but the weapon sway is too much. On around 60 fps is the sweet spot for how I want it to look universally but I don’t know how I would do that, and I also don’t have much understanding for DeltaTime as it is.

Alright I’ve worked on many FPS frameworks during my time on Roblox and during that time I’ve run into the exact same issue. Fortunately after about 2 years, (I should’ve done more research) I’ve figured out how to solve it and depending upon what you’re attempting to do. (ex: Keeping a constant position delta no matter the frame rate) I would personally start off by creating a variable that contains
dt * 60 (60 being the target framerate) then inside the spring’s shove function you need to multiply or divide the Vector3. To know when you need to multiply or divide you should know what you’re trying to avoid. If you’re trying to avoid excessive movement you multiply if you’re trying to prevent too little movement you divide. Let me know if this helps. Here’s an example of one of my frameworks where I employ this technique: Recoil + frame rate fix

2 Likes

Thank you for the solution. Below is a Pastebin for the edited basic SpringModule which uses springs locked at 60 frames a second no matter the FPS.
SpringModuleLocked60FPS

1 Like

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