How to make custom Camera Clipping for Custom Camera System

I’m trying to make Camera Clipping for my custom camera


I have tried several different methods but they all turn out very glitchy and unsmooth
Here is the code

runService.RenderStepped:Connect(function()	
	local Character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
	local rootPart = Character:FindFirstChild("HumanoidRootPart")
	if Character and rootPart then
		local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)*CFrame.Angles(0, 0, math.rad(zAngle))
		local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
		--Set camera focus and cframe
	
		local cameraCFrame = (startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z)))

		Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)

	end
end)

Any help is appreciated :grin:

pretty sure I’ve seen this camera code on the dev forum lol. Anyways what I did was just raycast

Alright so i ended up raycasting from the player to the designated camera cframe and set the new camera pos to the position of the raycast.

runService.RenderStepped:Connect(function()	
	local Character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
	local rootPart = Character:FindFirstChild("HumanoidRootPart")




	if Character and rootPart then




		local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)*CFrame.Angles(0, 0, math.rad(zAngle))
		local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
		--Set camera focus and cframe
		-- camera clipping
		local cameraCFrame = (startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z)))
		
		local raycastparams = RaycastParams.new()
		raycastparams.FilterDescendantsInstances = {workspace.Map}
		raycastparams.FilterType = Enum.RaycastFilterType.Whitelist
		local raycast = workspace:Raycast(Character.Head.Position,(cameraCFrame.Position - Character.Head.Position)*Vector3.new(1.2,1.2,1.2),raycastparams)
		
		if raycast then
			local founddis = (raycast.Position - Character.Head.Position)
			local foundpos = Character.Head.Position + founddis.Unit * (founddis.Magnitude - 1)
			Camera.CFrame = CFrame.new(foundpos,cameraFocus.p)
		else
			Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
		end
		
		
		
		



	end
end)