Help With Camera Bobbing

Problems with the bobbing camera

I have a small problem when executing the code,… in roblox studio it works normally but when it enters the same roblox to play the camera it bugs or the movement value is increased. Does anyone know why it happens? :disappointed_relieved:

Roblox Studio Camera Bobbing… :point_down:

Roblox Camera Bobbing… :point_down:

local runService = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local chr = plr.Character
local humrootpart = chr:WaitForChild("HumanoidRootPart")
local hum = chr:WaitForChild("Humanoid")
local cam = workspace.Camera

local tiltSpeedZ = 0.1
local bobbingSpeed = 0.1

local tilt = 0
local sinValue = 0

function lerp(a, b, t)
	return a + (b - a) * t
end

function calculateSine(speed, intensity)
	sinValue += speed 
	if sinValue > (math.pi * 2) then sinValue = 0 end
	local sineY = intensity * math.sin(2 * sinValue)
	local sineX = intensity * math.sin(sinValue)
	local sineCFrame = CFrame.new(sineX, sineY, 0)
	return sineCFrame
end

local previousSineX = 0
local previousSineY = 0
runService.RenderStepped:Connect(function(dt)
	local movementVector = cam.CFrame:vectorToObjectSpace(humrootpart.Velocity / math.max(hum.WalkSpeed, 0.01))
	local speedModifier = (hum.WalkSpeed / 16)
	tilt = math.clamp(lerp(tilt, movementVector.X * tiltSpeedZ, 0.1), -0.25, 0.1) 

	local sineCFrame = calculateSine(bobbingSpeed * speedModifier, movementVector.Z * speedModifier)
	local lerpedSineX = lerp(previousSineX, sineCFrame.X, 0.1)
	local lerpedSineY = lerp(previousSineY, sineCFrame.Y, 0.1)

	cam.CFrame *= CFrame.Angles(0, 0, tilt) * CFrame.new(lerpedSineX, lerpedSineY, 0)
	previousSineX = lerpedSineX
	previousSineY = lerpedSineY
end)

I don’t think it’s being increased, I think it’s a lag issue causing the exagerated movement.
Do you have this in a LocalScript in the player, or in a ServerScript?
If you run it locally for the player it should look like your Studio video, but if you are trying to get the server to run it you would experience some lag in the camera.

It’s in a local script located on the starting player character, there is only this local script, there is no other script running in the game. I tried it on a new baseplate but the result is the same. :frowning: