Camera Collision System Help

Hi.

I have made a draggable camera system and I also made a collision system to go along with it. Basically a “Collision Part” is fixed onto the players camera and if the collision part touches one of the walls you can’t drag it anymore. The issue is that when the wall stops the camera. The player can’t drag the camera back out the other way. The walls are meant to be boundaries for the camera.

local Run = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

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

local cameraCollisionPart = workspace:WaitForChild("CollisionPart")

local speed = 3
local lockMouse = true
local origin = CFrame.lookAt(Vector3.new(0,40,0), Vector3.new())
local moved = Vector2.zero

local dragging = false
local oldMousePos = Vector2.zero

cam.CameraType = Enum.CameraType.Scriptable

mouse.Button1Down:Connect(function()
	
	dragging = true
	oldMousePos = UIS:GetMouseLocation()
	
end)

mouse.Button1Up:Connect(function()
	
	dragging = false
	UIS.MouseBehavior = Enum.MouseBehavior.Default
	
end)

Run.RenderStepped:Connect(function(dt)
	
	for j,k in pairs(cameraCollisionPart:GetChildren()) do
		
		if not k:IsA("Part") then continue end
		
		for i,v in pairs(k:GetTouchingParts()) do

			if v.Parent.Name == "CollisionBoundaries" or v.Parent.Parent.Name == "CollisionBoundaries" then
				return
			end

		end
		
	end
	
	if dragging then
		if lockMouse then
			UIS.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
			moved += UIS:GetMouseDelta()*speed*dt
		else
			moved += (UIS:GetMouseLocation() - oldMousePos)*speed*dt
			oldMousePos = UIS:GetMouseLocation()
		end
	end
	
	cam.CFrame = origin * CFrame.new(-moved.X*speed, moved.Y*speed, 0)
	cam.CFrame = cam.CFrame * CFrame.Angles(math.rad(45),0,0)	
	
	cameraCollisionPart.CFrame = cam.CFrame
	
end)