Camera Lerp to make smooth camera(Topic Edited.)

See Below for the Issue! The Subject was slightly changed due I changed decided to make whole new script

Sorry, there’s not a ton of information here to help answer this—what are these variables actually used for? You never actually change the camera’s CFrame anywhere from what you’ve posted.

Well since the time I posted it, I decided to make the camera script from scratch and I have a new issue now, I need help with two things(Sorry for inconvenience)

First thing is the lerp, I wanna make the camera move smoothly when moving and zooming in and out, can you help me with that? I have zero idea…

Also I’d really appriciate if you explain it to me much as you can so I can learn

wait(2)


--SERVICES


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







--VARS


local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local HRP = Character.HumanoidRootPart

local Camera = workspace.CurrentCamera

type Degree = number

local pivotPosition: Vector3
local cameraPosition: Vector3
local pitch: Degree
local yaw: Degree
local currentMousePos: Vector2
local mouseScrollDelta: number
local mouseScrollInvoked: boolean
local mouseRotationInvoked: boolean
local pause: boolean



--Minimum Zoom
local minCameraScrollVector = Vector3.new(0, 4, 10)
--Max Zoom
local maxCameraScrollVector = Vector3.new(0, 5, 25)
local cameraZToYRatio = 2





--FUNCTIONS



local function init (): CFrame
	Camera.CameraType = Enum.CameraType.Scriptable

	--Rootpart's lookvector
	local HRPLookVec = HRP.CFrame.LookVector
	--Rootpart's Position
	local HRPPosition = HRP.Position

	local initialPosition = CFrame.new(HRPPosition, HRPPosition + HRPLookVec)
	local radPitch, radYaw, _ = initialPosition:ToOrientation() -- returns radians
	pitch = math.deg(radPitch)
	yaw = math.deg(radYaw)

	pivotPosition = initialPosition.Position
	cameraPosition = Vector3.new(0, 1, 9)
	currentMousePos = Vector2.new(0, 0)
	mouseScrollDelta = 0
	mouseScrollInvoked = false
	mouseRotationInvoked= false
	pause = false

	--Returns Cframe where character is and looks at
	return CFrame.new(pivotPosition) * CFrame.Angles(0, math.rad(yaw), 0) * CFrame.Angles(math.rad(pitch), 0, 0) * CFrame.new(cameraPosition) 
end


local function move ()
	pivotPosition = HRP.Position
end

local function limitPitch (pitchAng: Degree): Degree
	return math.clamp(pitchAng, -90, 90)
end

local function limitYaw (yawAng: Degree): Degreea

end

local function rotate(t: number)
	if not mouseRotationInvoked then 
		return
	end

	local difference = currentMousePos * -1
	local updateX = difference.X * t * 20 --20 is just a mouse sensitivity rn
	local updateY = difference.Y * t * 20 --same sens
	pitch = limitPitch(pitch + updateY)
	yaw += updateX

	mouseRotationInvoked = false
end

clampCameraVector = function(point: Vector3)
	return Vector3.new(
		point.X,
		math.clamp(point.Y, minCameraScrollVector.Y, maxCameraScrollVector.Y),
		math.clamp(point.Z, minCameraScrollVector.Z, maxCameraScrollVector.Z)
	)
end

interpolateCameraPosition = function(unit: number)
	return clampCameraVector(Vector3.new(0, unit, unit * cameraZToYRatio))
end
local function scroll (t: number)
	if not mouseScrollInvoked then
		return
	end

	local increment = t * 10--sensitivity
	if mouseScrollDelta < 0 then
		cameraPosition = clampCameraVector(cameraPosition + interpolateCameraPosition(increment))
	elseif mouseScrollDelta > 0 then
		cameraPosition = clampCameraVector(cameraPosition - interpolateCameraPosition(increment))
	end

	mouseScrollInvoked = false
end

local function render (t: number): CFrame
	move()
	rotate(t)
	scroll(t)
	HRP.CFrame = CFrame.new(pivotPosition) * CFrame.Angles(0, math.rad(yaw), 0)
	return CFrame.new(pivotPosition) * CFrame.Angles(0, math.rad(yaw), 0) * CFrame.Angles(math.rad(pitch), 0, 0) * CFrame.new(cameraPosition)
end


local initCFrame = init()
Camera.CFrame = initCFrame

local rayParams: RaycastParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = { Character }
local function collisionDetect (tempCameraCF: CFrame)
	local initial = Character.Head.Position
	local final = tempCameraCF.Position

	local result = workspace:Raycast(initial, final - initial, rayParams)
	if not result then
		return tempCameraCF
	end
	return (tempCameraCF - (tempCameraCF.Position - result.Position)) + (initial - tempCameraCF.Position).Unit
end



RunService.RenderStepped:Connect(function(deltaTime: number)
	if not pause then
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		local cameraCF = render(deltaTime)
		local detected = collisionDetect(cameraCF)
		Camera.CFrame = detected
	end
end)





--INPUTS

UserInputService.InputChanged:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if gameProcessedEvent then return end
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		currentMousePos = UserInputService:GetMouseDelta()
		mouseRotationInvoked = true
	end
end)

UserInputService.InputChanged:Connect(function(input: InputObject, gameProcessedEvent: boolean) 
	if gameProcessedEvent then return end
	if input.UserInputType == Enum.UserInputType.MouseWheel then
		mouseScrollDelta = input.Position.Z
		mouseScrollInvoked = true
	end
end)

UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.LeftAlt then
		pause = true
		UserInputService.MouseBehavior = Enum.MouseBehavior.Default
	end
end)

UserInputService.InputEnded:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.LeftAlt then
		pause = false
	end
end)