Need help with over the shoulder camera clipping through walls

Hello, so I am having a problem with my script for a over the shoulder system i got, its clipping through walls when i get close to them and i have no clue how to fix this, script below.

local players = game:GetService("Players")
local contextactionservece = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
--------
local Camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local cameraOffset = Vector3.new(1.5, 2, 8)
-----------------------------------------------------------------------
player.CharacterAdded:Connect(function(character)
	-- actual camera function, i.e positioning
	
	local humanoid = character:WaitForChild("Humanoid")
	local rootPart = character:WaitForChild("HumanoidRootPart")
	
	local cameraAngleX = 0
	local cameraAngleY = 0
	
	--humanoid.AutoRotate = false
	
	local function playerInput (actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Change then
			
			cameraAngleX -= inputObject.Delta.X
			cameraAngleY = math.clamp(cameraAngleY - inputObject.Delta.Y * 0.4, -75, 75)
			
		end
	end
	
	contextactionservece:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
	
	RunService:BindToRenderStep("CameraUpdate", Enum.RenderPriority.Camera.Value, function()
		
		local startCFrame = CFrame.new(rootPart.CFrame.Position) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
		local cameraCFrame = startCFrame:PointToWorldSpace(cameraOffset)
		local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -1000000))
		
		Camera.CFrame = CFrame.lookAt(cameraCFrame, cameraFocus)
		
		--local lookingCFrame = CFrame.lookAt(rootPart.Position, Camera.CFrame:PointToWorldSpace(Vector3.new(0, 0, -1000000)))
		--rootPart.CFrame = CFrame.fromMatrix(rootPart.Position, lookingCFrame.XVector, rootPart.CFrame.YVector)
		--
		
	end)
	
end) 
------------------------------------------------------------------------
local function focusControl(actionName, inputState, inputObject)
	--mouse control / locking center
	if inputState == Enum.UserInputState.Begin then
		
		Camera.CameraType = Enum.CameraType.Scriptable
		
		UserInput.MouseBehavior = Enum.MouseBehavior.LockCenter
		--UserInput.MouseIconEnabled = false   //MAKE MOUSE GO ICON GO POOF
		
		contextactionservece:UnbindAction("FocusControl")
	end
end

contextactionservece:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)

https://gyazo.com/ac729b3899376ce7c0be39b9dae619da

I found a solution in this forum:

1 Like

how were you able to implement this, I tried to do this too but I have to know Idea how to.

local function updateCamera()
	if character then
		local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
		if humanoidRootPart and shift.PlaybackState == Enum.PlaybackState.Completed then
			local startCFrame = CFrame.new(humanoidRootPart.CFrame.Position) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
			local camCFrame = startCFrame:PointToWorldSpace(cameraOffset)
			local cameraFocus = startCFrame:PointToWorldSpace(Vector3.new(cameraOffset.X, cameraOffset.Y, -1000000))
			local Character = game.Players.LocalPlayer.Character
			local Camera = game.Workspace.CurrentCamera

			local CameraRay = Ray.new(Character.Head.Position, Camera.CFrame.Position - Character.Head.Position)
			local Ignore = {Character}

			local HitPart, HitPosition = game.Workspace:FindPartOnRayWithIgnoreList(CameraRay, Ignore)
			
			if HitPart then
				print("HIT")
				camera.CFrame = CFrame.lookAt(camCFrame, cameraFocus) + (Camera.CFrame - (Camera.CFrame.Position - HitPosition)) + (Character.Head.Position - Camera.CFrame.Position).Unit
			else
				camera.CFrame = CFrame.lookAt(camCFrame, cameraFocus)
				end
				
		

			local lookingCFrame = CFrame.lookAt(humanoidRootPart.Position, camera.CFrame:PointToWorldSpace(Vector3.new(0, 0, -1000000)))
			humanoidRootPart.CFrame = CFrame.fromMatrix(humanoidRootPart.Position, lookingCFrame.XVector, humanoidRootPart.CFrame.YVector)
		end
	end
end