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!