How do I fix this snappy camera bobble?

Hey fellow Devs! So basically I’m trying to make a camera bobble (Camera moves/shakes when the player is moving), but I’m having a little problem. The bobble effect looks great and is how I want it, the problem is that the transition between walking and stopping is quite snappy (The camera simply teleports).

I was thinking of doing a tween but I’m not sure of how it would impact performances, and also, I’m pretty sure there’s a better way to do it. I tried lerping but I’m struggling to implement it into my code.

Here’s my code:

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local camera = workspace.CurrentCamera

local totalTime = 0

run.RenderStepped:Connect(function(deltaTime)
	totalTime += deltaTime
	
	local xSpeed = 0
	local ySpeed = 0
	local rotationSpeed  = 0
	local xAmount = 0
	local yAmount = 0
	local rotationMax = 0
	
	local x = 0
	local y = 0
	local rotation = 0
	
	local moveDirection = humanoid.MoveDirection
	if moveDirection.Magnitude > 0 then
		xSpeed = 5
		ySpeed = 5
		rotationSpeed = 5
		
		xAmount = .25
		yAmount = .25
		rotationMax = 1
		
		x = math.sin(totalTime * xSpeed) * xAmount
		y = math.abs(math.cos(totalTime * ySpeed)) * yAmount
		rotation = math.sin(totalTime * rotationSpeed) * rotationMax
	end
	
	humanoid.CameraOffset = Vector3.new(x, y, 0)
	camera.CFrame = camera.CFrame * CFrame.Angles(0,0,math.rad(-rotation))
end)

And here’s what it looks like:

Thank you to anyone who can help me find a solution to my problem!

Try replacing this line with:

camera.CFrame = camera.CFrame:Lerp(camera.CFrame * CFrame.Angles(0, 0, math.rad(-rotation)), deltaTime)
1 Like