My Camera Sway Constantly Moves

I’m making a horror game and want a Realistic Camera Sway Mechanic in the game.

I used Roblox’s new AI to see it could make it and so far… It’s done a really good job.

But the one issue I found, is that for some odd reason the camera continues to Sway even though I’m not moving the mouse anymore.

I’ve tried to fix this myself, but I don’t have a enough scripting knowledge to make it work good enough.


Code:

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local camera = workspace.CurrentCamera

local swaySpeed = 0.05 -- Adjust this value to change the speed of the sway
local swayAmount = 0.005 -- Adjust this value to change the amount of sway
local maxSway = 0.01 -- Adjust this value to limit the maximum sway

local targetSway = Vector2.new()
local currentSway = Vector2.new()

UserInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		local swayChange = Vector2.new(input.Delta.X, input.Delta.Y) * swayAmount
		targetSway = targetSway + swayChange
		targetSway = Vector2.new(math.clamp(targetSway.X, -maxSway, maxSway), math.clamp(targetSway.Y, -maxSway, maxSway))
	else
		targetSway = Vector2.new(0, 0)
	end
end)

RunService.RenderStepped:Connect(function()
	currentSway = currentSway:Lerp(targetSway, swaySpeed)
	local swayCFrame = CFrame.Angles(-currentSway.Y, -currentSway.X, 0)
	camera.CFrame = camera.CFrame * swayCFrame
end)

Any help would be much appreciated. Thank You for your time!

Hello!

You probally need to check if the humanoid is walking in the RenderStepped event.
You can check if putting at the start:

if Humanoid.MoveDirection.Magnitude < 1 then return end

If it didn’t work, please let me know.

I’m confused. Where am I supposed to put this. There’s no Humanoid Variable so it just pops up as a unknown global.

Can you specify what I should do with this line of code?

You need to get Humanoid using:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local Humanoid = character:WaitForChild("Humanoid")

I have a camera sway script i’m basing from it.