Deprecated view bobbing script - how to update?

Good few years ago I put this inside my game to add a camera bobbing script, which to this day’s been working fine.
Now as of recently, with roblox’ FPS unlocker update allowing higher FPS, the deltatime is so low that it becomes jittery. How would I make the bobbing function also for FPS over 60?

local player = game:GetService("Players").LocalPlayer
local character = script.Parent
local hum = character:WaitForChild("Humanoid")
local camera = game.Workspace.CurrentCamera

Z = 0

damping = hum.WalkSpeed / 2

PI = 3.1415926

tick = PI / 2

running = false
strafing = false

character.Humanoid.Strafing:connect(function(bool)
	strafing = bool
end)

character.Humanoid.Jumping:connect(function()
	running = false
end)

character.Humanoid.Swimming:connect(function()
	running = false
end)

character.Humanoid.Running:connect(function(speed)
	if speed > 0.1 then
		running = true
	else
		running = false
	end
end)

function mix(par1, par2, factor)
	return par2 + (par1 - par2) * factor
end

while true do
	game:GetService("RunService").RenderStepped:wait()
	
	fps = (camera.CoordinateFrame.p - character:WaitForChild("Head").Position).Magnitude
	
	if fps < 0.52 then
		Z = 1
	else
		Z = 0
	end
	
	if running == true and strafing == false then
		tick = tick + character.Humanoid.WalkSpeed / 92 --Calculate Bobbing speed.
	else
		if tick > 0 and tick < PI / 2 then
			tick = mix(tick, PI / 2, 0.9)
		end
		if tick > PI / 2 and tick < PI then
			tick = mix(tick, PI / 2, 0.9)
		end
		if tick > PI and tick < PI * 1.5 then
			tick = mix(tick, PI * 1.5, 0.9)
		end
		if tick > PI * 1.5 and tick < PI * 2 then
			tick = mix(tick, PI * 1.5, 0.9)
		end
	end
	
	if tick >= PI * 2 then
		tick = 0
	end	
	
	camera.CoordinateFrame = camera.CoordinateFrame * 
		CFrame.new(math.cos(tick) / damping, math.sin(tick * 2) / (damping * 2), Z) * 
		CFrame.Angles(0, 0, math.sin(tick - PI * 1.5) / (damping * 30)) --Set camera CFrame
end

When changing game:GetService("RunService").RenderStepped:wait() (the time becomes smaller with higher frames per second) to a number or anything, the bobbing as a whole stops too.

2 Likes

this is abit confusing.

but i think u just the delete the while true do,

then turn the render stepped into a function.

and use Lerp to make it framerate independant using the formula:

a - start value
b - result value
f - time
a = lerp(a, b, 1 - f ^ dt)

It can be easily adjusted to a vector lerp:
local currentVector = currentVector:Lerp(targetVector, 1 - howFastItLerps ^ deltaTime)

2 Likes

Hm yes, that should work. All the tick being changed confuses a bit, but it creates for a very nice curving camera bob, where it adjusts in the process to create a nice curve. Can that also be established with lerping - where it takes into account a curve, rather than a straight line?

for curves i think u could use sine and cos, though im sorry because im not familiar with sine and cos so you might have to do your own research about that.

Thr function mix is basically a lerp function.

To make lerp function framerate independent you use this specific formula.

Here is a good blog post on it.

https://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/

1 Like
runService.RenderStepped:Connect(function(dt)
	
end)

I eventually settled with this and incorporated the deltatime this way.

tick = tick + character.Humanoid.WalkSpeed / ((1/dt)*1.5)

Only thing is that the bobbing isn’t as rich as the original. I then changed the final damping (*30) to a lower number, allowing it to curve a bit more. Unfortunately it still bopped a bit too rapid, after which I tweaked the speed and other numbers again.

image

Despite it still not feeling 100% identical, I think it’s fixed.

Thanks for the thoughts and posts ^^