How to make the camera stop spinning?

The code below makes it so when you rotate the camera it spins smoothly. The only problem is that the camera keeps on spinning and it never stops. I want to add something in the script so the camera decelerates once the camera has stopped accelerating. In other words: I want the camera to slowly stop spinning instead of it spinning forever. How do I achieve this? The code below is in a local script in StarterGui.

local run = game:GetService'RunService'
local uis = game:GetService'UserInputService'

local cam = workspace.Camera
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local mult = 180/math.pi
local clamp = math.clamp

local current = Vector2.zero
local targetX, targetY = 0, 0

local speed = 0.5 -- Set the speed of the animation
local sensitivity = 1 -- Set the sensitivity of the rotation

uis.MouseDeltaSensitivity = 0.01
cam.CameraType = Enum.CameraType.Custom

run:BindToRenderStep("SmoothCam", Enum.RenderPriority.Camera.Value-1, function(dt)
	local delta = uis:GetMouseDelta()*sensitivity*100
	targetX += delta.X
	targetY = clamp(targetY+delta.Y,-90,90)
	current = current:Lerp(Vector2.new(targetX,targetY), dt*speed)
	cam.CFrame = CFrame.fromOrientation(-current.Y/mult,-current.X/mult,0)
end)

I discovered this equation though this video a while ago. I’m not entirely sure if it’s the effect you need but it allows for some dampening to occur as a value nears its target.

local goal --The goal value to be reached

currentPositon = currentPositon + (goal-currentPositon) * math.min(dt*1,1) 

--Multiply dt to effect speed
1 Like

I added that equation into the script and it works! The camera now gradually slows down to a stop. Thank you :slightly_smiling_face:.

1 Like

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