How do I stop the camera from going into walls?

I am trying to create camera collision on this custom camera script but I don’t know much about it. The one I made kind of works but when I go close to a wall it still clips through the wall. It also goes in and out at a certain distance. How can I fix this? Is there any topics I can read similar to this?

local defaultcam = Vector3.new(3,0.1,8)
local cameraPos = Vector3.new(3,0.1,8)
function updatebobble()
	local currentTime = tick()
	if humanoid.MoveDirection.Magnitude <= 0 then
		if aim == false then
			local bobblex = math.cos(currentTime * 2) * .01
			local bobbley =math.sin(currentTime * 2) * .01
			local bobble = Vector3.new(bobblex + 1,bobbley * 2,1)
			cameraPos = cameraPos:Lerp(defaultcam * bobble, .25)
		end
runService.RenderStepped:Connect(function()
    updatebobble()
	local Character = localPlayer.Character
	local rootPart = Character:FindFirstChild("HumanoidRootPart")
	if Character and rootPart and humanoid.Health > 0 then
		if aim == false 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)
			local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z ))
			local distance = (cameraCFrame.p-rootPart.Position).magnitude + 5 -- +5 to make sure it hits a    wall if the camera is in it
			local ray = Ray.new(rootPart.Position, (cameraCFrame.p-rootPart.Position).unit*distance)
			local hit, pos, norm, mat = game.Workspace:FindPartOnRayWithIgnoreList(ray, {rootPart.Parent})
			if hit then
				print(distance)
				defaultcam = Vector3.new(3,0.1,8- distance/4)
			else
				defaultcam = Vector3.new(3,0.1,8)
			end
			
			
			
			local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
				---
			local Character = game.Players.LocalPlayer.Character
			Camera.CFrame = CFrame.new(Vector3.new(cameraCFrame.X,cameraCFrame.Y,cameraCFrame.Z ),cameraFocus.p)
			
			--Set camera focus and cframe
		end
2 Likes

I made it a little better but it still goes through the wall.

local currenttime = os.time()
			local waittime = .1
			local lasthitt = currenttime - lasthit
			
			
			local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z ))
			local cameraCFrame2 = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(defaultcam.X,defaultcam.Y,cameraPos.Z ))
			local distance = (cameraCFrame2.p-Character.Head.Position).magnitude+5   -- +5 to make sure it hits a    wall if the camera is in it
			local ray = Ray.new(Character.Head.Position, (cameraCFrame2.p-Character.Head.Position).unit*distance)
			local hit, pos, norm, mat = game.Workspace:FindPartOnRayWithIgnoreList(ray, {rootPart.Parent})
			local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))
			local distance2 = (cameraCFrame.p-pos).magnitude
			if hit  then
				lasthit = currenttime
				print(distance2)
				defaultcam = Vector3.new(3,0.1,distance2*8/8)-- I use random numbers
			else
				if lasthitt >waittime then
					defaultcam = Vector3.new(3,0.1,8)
				end
			end