Custom camera gets messed up when colliding

I’m working on a custom camera and it works fine… until the player collides with parts, which then causes the camera to mess up. The more and more a player collides with objects, the more and more the camera is mispositioned.

Camera before collision:

After Collision:

Camera Script (in startercharacterscripts):

local UIS = game:GetService("UserInputService")

local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable

local mouse = game.Players.LocalPlayer:GetMouse()

local c = script.Parent

local hum = c:WaitForChild("Humanoid")
hum.AutoRotate = false

local hrp = c:WaitForChild("HumanoidRootPart")

local x = 0
local y = 0

local offset =,1,8,7)

UIS.InputChanged:Connect(function(input, processed)
	if processed then return end
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		x = x - input.Delta.X
		y = math.clamp(y - input.Delta.Y*0.4,-75,75)
		hrp.CFrame = hrp.CFrame * CFrame.Angles(0,math.rad(-input.Delta.X), 0)

	if game.Players:GetPlayerFromCharacter(c).PlayerGui.StartMenu.PlayBtn.Visible == true or game.Players:GetPlayerFromCharacter(c).PlayerGui.Customization.CharacterFrame.Visible == true then return end
	UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
	local startCFrame = * CFrame.Angles(0,math.rad(x),0) * CFrame.Angles(math.rad(y),0,0)
	local camCFrame = startCFrame:ToWorldSpace(,offset.Y, offset.Z))
	local camDirection =  startCFrame:ToWorldSpace(,offset.Y,-10000))
	cam.CFrame =, camDirection.Position)
	mouse.Icon = "rbxassetid://5216789017"