Help with Camera panning

I’m trying to make camera pan toward the nearest enemy
for camera system I have a new part that stick to player’s torso with mortor6D and act as a camera focus point

but as you can see in the video, the code I make just slide the mortor6d.C0 forward the same amount of the distance and when you getting out of the range the cam just broke entirely
you can say I have no idea what I’m doing please send help and explain what I have to do

my current code

-- Services
local PlayerService = game:GetService("Players")
local RunService = game:GetService("RunService")
local CS = game:GetService("CollectionService")

-- Variables
local Player = PlayerService.LocalPlayer
local CurrentCamera = workspace.CurrentCamera
local Angle = -50

-- new focus point
local focusPoint = Instance.new("Part")
focusPoint.Anchored = false
focusPoint.Parent = Player.Character:FindFirstChild("Torso")
focusPoint.CanCollide = false
focusPoint.CanQuery = true
focusPoint.CanTouch =false
focusPoint.Size = Vector3.new(1,1,1)
focusPoint.Transparency = 1

-- create a mortor6d and connect focuspoint to the players torso
local motor6d = Instance.new("Motor6D")
motor6d.Parent = focusPoint
motor6d.C0 = CFrame.new(0, 3.5, 0)
motor6d.Part0 = focusPoint
motor6d.Part1 = Player.Character.Torso

-- Cause Camera Lag
local lagFactor = 0.04 -- adjust lag (1=No Lag, 0 = Broken, 0.01 = Lot of lag)

local lastPosition = Vector3.new()
local lastRotation = CFrame.new()

CurrentCamera.CameraType = Enum.CameraType.Scriptable

RunService.RenderStepped:Connect(function(deltaTime)
	if Player.Character then
		local focus = focusPoint
		if focus then

			-- lock the camera and make it follow player
			local targetPosition = focus.Position + Vector3.new(0, 20, 15)
			local targetRotation = CFrame.Angles(math.rad(Angle), 0, 0)

			-- apply cam lag
			lastPosition = lastPosition:Lerp(targetPosition, lagFactor)
			lastRotation = lastRotation:Lerp(targetRotation, lagFactor)
			
			-- where I start my attempt at making camera pan towatd nearest enemy
			local range = 35
			local closest

			for _, enemy in CS:GetTagged("Enemy") do
				local distance = (Player.Character.HumanoidRootPart.Position - enemy.Position).Magnitude
				

				if distance < range then
					motor6d.C0 = CFrame.new(0, 0, distance)
				else
					return
				end
			end
			
			CurrentCamera.CFrame = CFrame.new(lastPosition) * lastRotation

			
			-- cam collision to prevent the cam from clipping through the wall
			local cameraRay = Ray.new(Player.Character.HumanoidRootPart.Position, CurrentCamera.CFrame.Position - Player.Character.HumanoidRootPart.Position)
			local Ignore = {Player.Character,focusPoint}
			
			-- check if part has the tag tag if yes, don't collide the cam with the part
			for _, Object in CS:GetTagged("Prop") do
				table.insert(Ignore, Object)
			end
			
			for _, Object in CS:GetTagged("Item") do
				table.insert(Ignore, Object)
			end
			
			for _, Object in CS:GetTagged("Enemy") do
				table.insert(Ignore, Object)
			end

			local HitPart, HitPosition = game.Workspace:FindPartOnRayWithIgnoreList(cameraRay, Ignore)

			CurrentCamera.CFrame = (CurrentCamera.CFrame - (CurrentCamera.CFrame.Position - HitPosition)) + (Player.Character.HumanoidRootPart.Position - CurrentCamera.CFrame.Position).Unit 
		end
	end
end)

I’m really confused by what your end goal is, I’m sorry but can you re-explain it with more details

Use “AlignOrientation” and “AlignPosition” it will work properly

I want to pan the camera toward a nearest part with certain tag so both that part and player are on the same screen

no part:
camera lock onto focus point

part in range:
move the focus point toward the part

I just looking at it and yeah this might work but can it be use on a part that I already connect the motor6d