Camera Lerping Stutter

Hello,
So I’ve been working on something with a smooth camera feature which basically has a smooth transition in rotating the camera. An issue I’ve had with it is that if you move your camera angle too fast, it bugs out and stutters. This has happened not only to me but with others before due to ROBLOX’s lerping.
Phenomenon:


In this example, you can see the camera stutters whenever I do a 360 quickly.
Is there any way to fix this?

Code:

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

local Player = Players.LocalPlayer

local Config = Player:WaitForChild("Configuration")
local SettingsConf = Player:WaitForChild("Settings")

local LerpSettings = SettingsConf:WaitForChild("Lerp")
local LerpValue = LerpSettings:WaitForChild("Value")

local Binds = ReplicatedStorage:WaitForChild("Binds")

local SetMouseLock = Binds:WaitForChild("SetMouseLock")
local CameraBind = Binds:WaitForChild("CameraEnabled")

local CurrentCamera = workspace.CurrentCamera

local LockCenter = Enum.MouseBehavior.LockCenter
local Scriptable = Enum.CameraType.Scriptable
local CameraP = Enum.RenderPriority.Camera
local Default = Enum.MouseBehavior.Default

local CFAng = CFrame.Angles
local CFNew = CFrame.new

local V3New = Vector3.new

local clamp = math.clamp
local rad = math.rad

local Offset = CFNew(V3New(0, 1.65))
local ClampYAmount = 80
local SensDiv = 4

local CameraEnabled = true
local MouseLocked = true

local CameraRot =
{
	X = Config:WaitForChild("RotX");
	Y = Config:WaitForChild("RotY");
}

RunService:BindToRenderStep("CameraWork", CameraP.Value, function(dT)
	local Scale = dT * 60
	
	UserInputService.MouseBehavior = MouseLocked and LockCenter or Default
	CurrentCamera.CameraType = Scriptable
	
	if CameraEnabled then
		local Delta = UserInputService:GetMouseDelta() / SensDiv
		CameraRot.Y.Value = clamp(CameraRot.Y.Value - Delta.Y, -ClampYAmount, ClampYAmount)
		CameraRot.X.Value = CameraRot.X.Value - Delta.X
	end
	
	local Character = Player.Character
	local RootPart = Character and Character:FindFirstChild("HumanoidRootPart")
	
	if RootPart then
		local RootPos = CFNew(RootPart.Position) * Offset
		
		local LerpCF = CurrentCamera.CFrame:Lerp(RootPos * CFAng(0, rad(CameraRot.X.Value), 0) * CFAng(rad(CameraRot.Y.Value), 0, 0), LerpValue.Value * Scale)
		local CurrentX = CameraRot.X.Value
		
		local LerpLV = LerpCF.LookVector
		
		local RootReal = RootPos.Position
		local NewRoot = CFNew(RootReal, RootReal + LerpLV)
		
		CurrentCamera.CFrame = NewRoot
		
		for _,v in pairs(Character:GetDescendants()) do
			if v:IsA("BasePart") then
				v.LocalTransparencyModifier = 1
			end
		end
	end
end)

SetMouseLock.Event:Connect(function(Locked)
	MouseLocked = Locked
end)
CameraBind.Event:Connect(function(Enabled)
	CameraEnabled = Enabled
end)

Thanks.

3 Likes