Camera Sway Script Not Working?

Pretty basic script, it’s just meant to sway the camera when you turn you’re camera. It however, doesn’t work. Any idea on why this is?:

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

-- Player & Camera --
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera

-- Variables --
local turnAngle = 0

-- Lerp Function --
local function lerp(a, b, t)
	return a + (b - a) * t
end

-- Camera Sway --
RunService:BindToRenderStep("CameraSway", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
	local mouseDelta = UserInputService:GetMouseDelta()

	turnAngle = lerp(turnAngle, math.clamp(mouseDelta.X, -6, 6), 6 * deltaTime)

	Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, math.rad(turnAngle))
end)

It works, it’s just that the delta in the lerp is insanely low… you put it as 6 * deltatime, and when you have 60 fps, then it’s 0.016

replace it with

local lerpspeed = 50
lerp(start,end,1 - math.exp(-lerpseed*deltatime)

Sorry I didn’t get back to you sooner, can you send me you’re version of the script with that addition. I might just be confused but I tried to add it and it didn’t work.

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

-- Player & Camera --
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera

-- Variables --
local turnAngle = 0

-- Lerp Function --
local function lerp(a, b, t)
	return a + (b - a) * t
end

-- Camera Sway --
RunService:BindToRenderStep("CameraSway", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
	local mouseDelta = UserInputService:GetMouseDelta()
	local lerpspeed = 50

	turnAngle = lerp(turnAngle, 0, 1 - math.exp(-lerpspeed * deltaTime))

	Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, math.rad(turnAngle))
end)

Here, I’ve read your script and the problem is well… because the mousedelta isn’t being used, at all!

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

-- Player & Camera --
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera

-- Variables --
local turnAngle = 0

-- Lerp Function --
local function lerp(a, b, t)
	return a + (b - a) * t
end

-- Camera Sway --
RunService:BindToRenderStep("CameraSway", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
	local mouseDelta = UserInputService:GetMouseDelta()
	local lerpspeed = 50

	turnAngle = lerp(turnAngle, mouseDelta.X*10, 1 - math.exp(-lerpspeed * deltaTime))

	Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, math.rad(turnAngle))
end)
1 Like

I got a script that actually works :3

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

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

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

RunService:BindToRenderStep("CameraSway", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
	local MouseDelta = UserInputService:GetMouseDelta()

	Turn = Lerp(Turn, math.clamp(MouseDelta.X, -6, 6), (6 * deltaTime))
	Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, math.rad(Turn))
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.