Camera Sway :Lerp()

How can I set the camera to move and adjust smoothly and efficiently?

I tried this solution among many others:

--[[LocalScript: Inside of StarterPlayerScripts]]--
local Camera = workspace.CurrentCamera

local mult = 1
lastCameraCF = Camera.CFrame

function RenderLoop() 
	local rotation = Camera.CFrame:toObjectSpace(lastCameraCF) 
	local x,y,z = rotation:ToOrientation() 
	swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1) 
	lastCameraCF = Camera.CFrame --update the last cframe
end

NOTE: I replaced ‘swayOffset’ with ‘Camera.CFrame’ because…
However I don’t know what to set swayOffset to…

The desired sway works, however, the camera re-center’s in one direction as shown below.

Image from Gyazo

Also, my camera seems to be rooted at the spawn, this only occurs with the above code…

Image from Gyazo

I’d appreciate a fix to this, or a reliable alternative.

Edits
  • Removed blurred comment from post. Going to sleep now, will be back in the morning looking for answers!

  • Added the script type and location above example. –[[LocalScript: Inside of StarterPlayerScripts
    ]]–

  • Added variable Camera to example code. local Camera = workspace.CurrentCamera

I understand this is unnecessary, I just wanted to do it :hearts:

1 Like

Good morning, still looking for a solution. Any help or direction would be appreciated!

What is the camera variable and what type of script is this on?

It is in a localscript, located in the StarterPlayerScripts.

local Camera = workspace.CurrentCamera

Have you tried printing out rotation and x y z? Also make sure you are multiplying Camera.CFrame by swayOffset

That’s my problem, I don’t know what to set swayOffset to.

Both the rotation and x y z print.
Rotation:

X/Y/Z:
xyz

In the RenderLoop, you are setting the swayOffset already. All you need to do is set camera.Cframe to camera.cframe*swayoffset

Hi, the writer of the original piece. Try setting swayOffset to CFrame.new() and not Camera.CFrame

I’ll update the original post since this seems to be a common issue whenever this code gets referred to.