3rd Person Camera

Hi there. These past few days I’ve been working on an over the shoulder 3rd person camera system and it’s been going well however there are some flaws.

The fatal flaw is the collisions. It works in studio somewhat…

External Media

However in game it’s having some weird behaviour.

External Media

This is the code I use to set the camera CFrame:

-- Check intersection
local function Check_Intersection(Centre: Vector3, Position: Vector3): RaycastResult
	local result: RaycastResult = workspace:Raycast(Centre, (Position - Centre), RAYCAST_PARAMS)
	
	if result then
		local instance: Instance = result.Instance
		
		if instance.CanCollide then
			return result
		else
			return nil
		end
	else
		return nil
	end
end

-- Main
local camera_CFrame: CFrame = start_CFrame + start_CFrame:VectorToWorldSpace(Vector3.new(self._x_offset, 0, 4.5))
	local camera_focus: CFrame = start_CFrame + start_CFrame:VectorToWorldSpace(Vector3.new(self._x_offset, 0, -100000))
	
	camera.CFrame = CFrame.lookAt(camera_CFrame.Position, camera_focus.Position)
	local intersection = Check_Intersection(self.Head:GetPivot().Position, camera.CFrame.Position)
	if intersection then
		local height: number = math.tan((camera.FieldOfView * 2) / camera.NearPlaneZ)
		local factor: number = math.sqrt((height * 2)^2 + (((height^2) * (mouse.ViewSizeX / mouse.ViewSizeY)^2)))

		camera.CFrame = (camera.CFrame - (camera.CFrame.Position - intersection.Position) - (self.Head.Position - camera.CFrame.Position).Unit + (intersection.Normal.Unit * factor))
	end

Any help would be appreciated. Thanks for reading