Third Person anti-wall-clipping is rotating weirldy

I want to stop the clipping through the wall with the camera and stop turning
asynchronous

If i go with the camera into the ground the camera is weirdly rotating, and if i walk into a wall and keep turning my camera it will go off the “locked” position

For the third Person part i tried with raycasts to spot a mesh and if it does set the position of the camera to the hitpoint, for the weird rotations and the wall desync i have no idea of solving

local script in StarterCharacterScripts:

UserInputService.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*sensetivity, -75, 75)
		
		
		hrp.CFrame = hrp.CFrame * CFrame.Angles(0, math.rad(-input.Delta.X), 0)
	
		end

end)




game:GetService("RunService").Stepped:Connect(function()

	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	local startCFrame = CFrame.new(hrp.CFrame.Position) *CFrame.Angles(0, math.rad(x), 0)*CFrame.Angles(math.rad(y), 0 , 0) 
	local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(offset.X,offset.Y, offset.Z))
	local cameraDirection = startCFrame:ToWorldSpace(CFrame.new(offset.X,offset.Y, 0))
	
	cameraCFrame = CameraModule.anticameraclipping(hrp,cameraCFrame, offset, char, hitpoint, startCFrame)
	
	camera.CFrame = CFrame.new(cameraCFrame.Position, cameraDirection.Position)
	neck.C0 = CFrame.new(neck.C0.Position) * CFrame.Angles(math.rad(y), 0, 0)
	
	if isAiming and Rightshoulder then
		Rightshoulder.Transform = CFrame.new(Rightshoulder.Transform.Position) * CFrame.Angles(math.rad(y+90), 0, 0)
		Leftshoulder.Transform = CFrame.new(Leftshoulder.Transform.Position) * CFrame.Angles(math.rad(y+90), 0, 0)
		
	end
end)




Modular script:

function cameraModule.anticameraclipping(hrp, cameraCFrame, offset, char, hitpoint, startCFrame)
	local rayOrigin = hrp.CFrame.Position
	local rayDirection = cameraCFrame.Position - rayOrigin
	rayDirection = Vector3.new(rayDirection.X, rayDirection.Y, rayDirection.Z)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = {char}
	raycastParams.IgnoreWater = true

	local raycastResult = workspace:Raycast(rayOrigin,rayDirection, raycastParams)
	
	
	if raycastResult then
		print("hello")
		hitpoint = raycastResult.Position

	else 

		hitpoint = nil

	end
		
		if hitpoint then
			
			local originalCFrame = cameraCFrame
			cameraCFrame = nil
		cameraCFrame = CFrame.new(hitpoint.X,hitpoint.Y , hitpoint.Z) * CFrame.Angles(originalCFrame.Rotation.X,originalCFrame.Rotation.Y,originalCFrame.Rotation.Z)
			print("cameraZ after: ", cameraCFrame.Position.Z)
			local hitpartName = raycastResult.Instance.Name
			print(hitpartName)
			return cameraCFrame
		end

	return cameraCFrame
end

It would be great if smb could imagine of a solving approach and letting me know it