Using DeltaTime In ViewBobbing Script

I have this viewbobbing script that is based on a script from the toolbox and some of my own code added too. I realized that this script does not use DeltaTime so it lags on lower-end devices which looks really weird. I tried to use DeltaTime in the script but it broke the script and still wasn’t adjusting for lower framerates. If anyone knows something I could do to adjust for a lower framerate either using DeltaTime or another method I would greatly appreciate it.

local BobbingDamping = 1.4
local TiltDamping = 3
local OffsetDamping = 0.9

local RunService = game:GetService("RunService")
local Camera = game:GetService("Workspace").CurrentCamera
local Player = game:GetService("Players").LocalPlayer
if not Player.Character then
	
	Player.CharacterAdded:Wait()
	
end
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")

local Damping = (Humanoid.WalkSpeed / 2) * BobbingDamping
local Pi = math.pi
local Increment = Pi / 2
local IdleIncrement = 0

function Mix(Current, Change, Factor)
	
	return Change + (Current - Change) * Factor
	
end

while Character.Parent ~= nil do
	
	RunService.RenderStepped:Wait()
	
	if Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then
		
		IdleIncrement = 0
		Increment = (Increment + Humanoid.WalkSpeed / 92)
		
	else
		
		if Increment > 0 and Increment < Pi / 2 then
			
			Increment = Mix(Increment, Pi / 2, 0.9)
			
		end
		if Increment > Pi / 2 and Increment < Pi then
			
			Increment = Mix(Increment, Pi / 2, 0.9)
			
		end
		if Increment > Pi and Increment < Pi * 1.5 then
			
			Increment = Mix(Increment, Pi * 1.5, 0.9)
			
		end
		if Increment > Pi * 1.5 and Increment < Pi * 2 then
			
			Increment = Mix(Increment, Pi * 1.5, 0.9)
			
		end
		IdleIncrement = (IdleIncrement + Pi / 100)
		Camera.CFrame = Camera.CFrame * CFrame.new(0, math.sin(IdleIncrement) / (Pi / 0.14), 0) * CFrame.Angles(math.rad(math.sin(IdleIncrement) / (Pi / 0.04)), math.rad(math.sin(IdleIncrement) / (Pi / 0.02)), 0)
		
		if IdleIncrement >= Pi * 2 then
			
			IdleIncrement = 0
			
		end
		
	end
	
	if Increment >= Pi * 2 then
		
		Increment = 0
		
	end
	
	Camera.CFrame = Camera.CFrame * CFrame.new(math.cos(Increment) / Damping * OffsetDamping, math.sin(Increment * 2) / (Damping * 2 * OffsetDamping), 0) * CFrame.Angles(0, 0, math.sin(Increment - Pi * 1.5) / (Damping * 15 * TiltDamping))
	
end
1 Like

When using delta time, what are you trying to accomplish? Are you trying to make it adjust the bob angle more when delta time is higher and bob less if delta time is lower?

My goal is to increase/decrease the speed of the bobbing to account for different framerates. Basically, with lower framerates, I need to increase the speed since the framerate is lower and thus causes the bobbing to also run slower since it updates less.