I’m looking at improving some code I wrote a couple years ago for a project and something that has always irked me about this code is the fact that, when starting and stopping movement, the camera snaps from its resting position to whatever the output of the sine or cosine function is at that given time. Point being, I’d like the whole process from initiating movement and stopping movement to be smoother camera-wise. Below is my code, any tips?
function updateBobbleEffect()
local currentTime = tick()
if humanoid.MoveDirection.Magnitude > 0 then
if humanoid.WalkSpeed <= 7 then
local bobbleX = math.sin(currentTime * 4) * .20
local bobbleY = math.abs(math.sin(currentTime * 5)) * .20
local rotBobbleZ = math.sin(currentTime * 5) * .01
local bobble = Vector3.new(bobbleX, bobbleY, 0)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, .25)
workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(0,0,rotBobbleZ)
elseif humanoid.WalkSpeed > 7 then
local bobbleX = math.sin(currentTime * 7) * .20
local bobbleY = math.abs(math.sin(currentTime * 7)) * .20
local rotBobbleX = math.sin(currentTime * 10) * .001
local rotBobbleZ = math.sin(currentTime * 5) * .01
local bobble = Vector3.new(bobbleX, bobbleY, 0)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, .25)
workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(rotBobbleX,0,rotBobbleZ)
end
elseif humanoid.MoveDirection.Magnitude == 0 then
local bobbleY = math.abs(math.sin(currentTime * 1)) * .05
local rotBobbleZ = math.sin(currentTime * 0.01) * .001
local bobble = Vector3.new(0, bobbleY, 0)
humanoid.CameraOffset = humanoid.CameraOffset:lerp(bobble, .15)
workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(0,0,rotBobbleZ)
else
humanoid.CameraOffset = humanoid.CameraOffset * .75
end
end