How do I add camera sway?

I’m trying to let the camera sway when the player turns their camera.
Some people have suggested I use springs, and after some looking around I still have no clue how to implement such thing. Is there maybe a better way to do it?

1 Like

Can you elaborate a bit more? Charsss

Here is an example of what I mean:

The camera sways when the player turns.
Some people said I have to use a spring module and attach a spring to the head, yet I have no clue how.

Sorry forr keeping you waiting. Your problem seemed to be similar to this post. This is what you might be looking for. Tho if you want tl create it from scratch this is achieved using tweens.

There are quite a few topics on the forum about camera sway. Try using the Search tool up top to find how others have solved the issue in the past.

Yes that is true, but barely any tackle my problem. Posts that do try to tackle it, don’t go into details.

Sadly not, that adds some sort of bobbing effect for when you are walking, not exactly a camera sway.

You can use UserInputService:GetMouseDelta() to get mouse delta, use the X direction of the mouse delta for camera turn.

here is a sample code I made

--Services
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- Variables
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Turn = 0

-- Functions
local Lerp = function(a, b, t)
	return a + (b - a) * t
end;

-- Main
LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson -- First Person

RunService:BindToRenderStep("CameraSway", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
	local MouseDelta = UserInputService:GetMouseDelta()
	
	Turn = Lerp(Turn, math.clamp(MouseDelta.X, -6, 6), (15 * deltaTime))
	
	Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, math.rad(Turn))
end)
20 Likes

It works! But is this a bug? If i turn my graphics to full, The Swaying Effect glitches or switch.

That’s likely caused by lag with high delta times, which would be the reason to the massive amount of sway.

This should fix it!

--Services
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- Variables
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Turn = 0

-- Functions
local Lerp = function(a, b, t)
	return a + (b - a) * math.clamp(t, 0, 1)
end;

-- Main
LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson -- First Person

RunService:BindToRenderStep("CameraSway", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
	local MouseDelta = UserInputService:GetMouseDelta()
	
	Turn = Lerp(Turn, math.clamp(MouseDelta.X, -6, 6), (15 * deltaTime))
	
	Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, math.rad(Turn))
end)
6 Likes