Prevent custom camera going through objects?

I’m using a custom camera and I want to prevent a player from moving their camera into objects, and do similar to what Roblox does, where the camera basically zooms in if you move it over an object, that is either non-transparent or cancollide true
ezgif.com-gif-maker (65)

I’d assume I have to use a Raycast, but from there I wouldn’t know where to go/what to do

--// Update the camera's CFrame
local function UpdateCamera()	
	local Character = Player.Character
	if not Character then return end
	
	CurrentCamera.CameraType = Enum.CameraType.Scriptable
	
	local Positional = CFrame.new(Character.HumanoidRootPart.Position) * InitialCameraCFrame
	local CamRotation = CurrentCamera.CFrame - CurrentCamera.CFrame.Position
	local Rotational = CFrame.Angles(0, math.rad(-RotX), 0) * CFrame.Angles(math.rad(-RotY), 0, 0)
	
	local Offset = CFrame.new(0, 1, Zoom)
	
	local NewCameraCFrame = Positional * CamRotation:Lerp(Rotational, 0.75) * Offset
	
	CurrentCamera.CFrame = NewCameraCFrame
end

I was looking through the default camera controls, and I believe it was called Poppercam that Roblox use? But the code is a jumbled mess of modules all interlinked, and I can’t understand how any of it works.

1 Like