Need help with modifying FreeCamera to have collision

I’m trying to modify the FreeCamera script that’s built into the CoreGui and it’s all working fine, but I need a bit of help with adding collision to the freecam
I’ve tried using rays, but I can’t really understand them well, can someone lend me a hand?
These are the lines of the script I’ve been experimenting with most, assuming they handle the camera’s movement

local function StepFreecam(dt)
    local vel = velSpring:Update(dt, Input.Vel(dt))
    local pan = panSpring:Update(dt, Input.Pan(dt))
    local fov = fovSpring:Update(dt, Input.Fov(dt))

    local zoomFactor = sqrt(tan(rad(70/2))/tan(rad(cameraFov/2)))

    cameraFov = clamp(cameraFov + fov*FOV_GAIN*(dt/zoomFactor), 1, 120)
    cameraRot = cameraRot + pan*PAN_GAIN*(dt/zoomFactor)
    cameraRot = Vector2.new(clamp(cameraRot.x, -PITCH_LIMIT, PITCH_LIMIT), cameraRot.y%(2*pi))

    local cameraCFrame = CFrame.new(cameraPos)*CFrame.fromOrientation(cameraRot.x, cameraRot.y, 0)*CFrame.new(vel*NAV_GAIN*dt)
    cameraPos = cameraCFrame.p

    Camera.CFrame = cameraCFrame
    Camera.Focus = cameraCFrame*CFrame.new(0, 0, -GetFocusDistance(cameraCFrame))
    Camera.FieldOfView = cameraFov
end

the script can be found at https://pastebin.com/raw/MaXRQfKU