Smooth transition with View Bobbing

I am working on a view bobbing script and when the player stops moving, I want it to smoothly transition from the bobbing to the regular camera CFrame. so I did a lerp but it doesnt transition smoothly, it still cuts and changing the alpha value to a decimal below 1 is basically just decreasing the intensity of the shake.

local runService = game:GetService("RunService")
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera

local function UpdateBobble()
    local cT = tick()
    local targetAngle

    if humanoid.MoveDirection.Magnitude > 0 then
        local bobbleUpDown = math.cos(cT * 5) * 0.1
        local bobbleLeftRight = math.sin(cT * 5) * 0.05
        local bobbleTilt = math.sin(cT * 5) * 0.4
        local bobble = CFrame.new(0, bobbleUpDown, 0) * CFrame.Angles(0, math.rad(bobbleLeftRight), math.rad(bobbleTilt)) 

        targetAngle = camera.CFrame * bobble
        camera.CFrame = camera.CFrame:Lerp(targetAngle, 1)
    end
end

runService.RenderStepped:Connect(UpdateBobble)

you need to have the offset value set outside of the function to have it smoothly move back. there seems to be a weird camera jerk when you stop walking though so not sure what’s up with that

local runService = game:GetService("RunService")
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera

local currentOffset = CFrame.new()
local lastOffset = currentOffset
local targetOffset

local function UpdateBobble()
	local cT = tick()
	

	if humanoid.MoveDirection.Magnitude > 0 then
		local bobbleUpDown = math.cos(cT * 5) * 5
		local bobbleLeftRight = math.sin(cT * 5) * 5
		local bobbleTilt = math.sin(cT * 5) * 3
		
		targetOffset = CFrame.new(0, bobbleUpDown, 0) * CFrame.Angles(0, math.rad(bobbleLeftRight), math.rad(bobbleTilt))
	else
		targetOffset = CFrame.new()
	end
	
	if targetOffset then
		currentOffset = currentOffset:Lerp(targetOffset, 0.2)
	end
	
	camera.CFrame = camera.CFrame * lastOffset:Inverse() * currentOffset
	lastOffset = currentOffset
end

runService.RenderStepped:Connect(UpdateBobble)

actually just use this function instead, i just realized you were updating the position of the camera which was causing it to glitch

local function UpdateBobble()
	local cT = tick()
	
	if humanoid.MoveDirection.Magnitude > 0 then
		local bobbleUpDown = math.cos(cT * 10) * 1
		local bobbleLeftRight = math.sin(cT * 5) * 1
		local bobbleTilt = math.sin(cT * 5) * 5
		
		targetOffset = CFrame.Angles(math.rad(bobbleUpDown), math.rad(bobbleLeftRight), math.rad(bobbleTilt))
	else
		targetOffset = CFrame.new()
	end
	
	if targetOffset then
		currentOffset = currentOffset:Lerp(targetOffset, 0.2)
	end
	
	camera.CFrame = camera.CFrame * lastOffset:Inverse() * currentOffset
	lastOffset = currentOffset
end
1 Like

Thank you! The little camera jerk cut at the end of the original script was what I was trying to fix and I thought lerping might help. This works great though!

1 Like