How to detect if camera touches a part

Hello, i was making a script that makes the screen blurry when something touches the “water” part, so when the character’s head touches the “water” the screen gets blurry, but it isn’t realistic because if the camera is out of water and the character inside of it the screen stills blurry.

I tried to use game.Workspace.CurrentCamera but it doesn’t have the “touched” event

My question is: how can i detect when camera is inside this part?

(if you don’t understand something on this post or find something wrong like the category let me know because i don’t speak a good english and i’m new in the forum)

1 Like

You could use Region3, make a region around the camera, and if something touches the region, you print it out? Or make it do something.

Region3 based functions are being deprecated. Use the new spatial query API.

This script below will detect if the camera is inside a part.

local param = OverlapParams.new()
param.FilterType = Enum.RaycastFilterType.Whitelist
param.FilterDescendantsInstances = {workspace} --customize this, for example: {workspace.Ocean}

local cam = workspace.CurrentCamera
local hitbox = Vector3.new()

game:GetService('RunService').RenderStepped:Connect(function()
	local parts = workspace:GetPartBoundsInBox(cam.CFrame, hitbox, param)
	if #parts > 0 then
		print('camera inside part')
	end
end)
6 Likes

Didn’t know about that, awesome to see.

I’ll try to put this inside my script, thank you.

1 Like

It worked pretty well but every part the camera touches, screen gets blurry, how can i get properties of the part that the camera touched? Edit: Sorry for the late answers, you guys answered in the same hour but i was going to sleep

You should never need to add all of workspace as a whitelist, this forces an iteration through every part in the workspace to build the list.

GetPartBoundsInBox returns a table of the parts touching the box
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/GetPartBoundsInBox

I know about this table but i can’t get a specific member of this table, i searched in roblox api and i found table.find() but it didn’t worked with this table

That’s my script, when the camera is inside a part screen gets blurry but that shouldn’t work with every part in the game


local Parametro = OverlapParams.new()
Parametro.FilterType = Enum.RaycastFilterType.Whitelist
Parametro.FilterDescendantsInstances = {game.Workspace}

local Camera = game.Workspace.CurrentCamera

local DB = false
local DB2 = false
local DB3 = true

local function OnRenderStepped()

	local parts = workspace:GetPartBoundsInBox(Camera.CFrame, Vector3.new(), Parametro)
	
	if #parts > 0 then
		
		print(parts)
	end
	
	if #parts > 0 and DB3 == true then
		
		DB3 = false

		if game.Lighting:FindFirstChild("Blur") then

			game.Lighting.Blur.Size = 20

			DB2 = true
			DB = true

		else

			local BlurLocal = Instance.new("BlurEffect")

			BlurLocal.Parent = game.Lighting
			BlurLocal.Name = "Blur"
			BlurLocal.Size = 20

			DB2 = true
			DB = true
		end

		script:WaitForChild("WaterSplash"):Play()

		wait(0.1)

		script:WaitForChild("UnderWater"):Play()
		
	elseif #parts == 0 and DB3 == false then
		
		DB3 = true
		DB2 = false

		wait(0.15)

		script:WaitForChild("UnderWater"):Stop()
		script:WaitForChild("OutWaterSplash"):Play()

		if DB == true then

			DB = false

			while wait(0.01) do

				game.Lighting.Blur.Size = game.Lighting.Blur.Size - 0.3

				if game.Lighting.Blur.Size == 0 or game.Lighting.Blur.Size < 0 or DB2 == true then

					break
				end
			end
		end
	end
end

game:GetService("RunService").Stepped:Connect(OnRenderStepped)```

I actually used a function to return whether or not the Camera was above the waterline for something like this I had in one of my projects.

local function GetSeaHeight(Position: Vector3): number
    -- Funcion sinplified for sharing
    -- Usually I would have put some calculations here for perlin noise waves or gerster waves
    return 0 -- Water would be a flat plane
end)

RunService.RenderStepped:Connect(function(deltaTime)
    local SeaHeight = GetSeaHeight(Camera.CFrame.Position)
    if Camera.CFrame.Y > SeaHeight then 
        -- Camera Overwater
    else
        -- Camera Underwater
    end

end)

What is more you can actually create “depth” effect by checking the distance the camera is underwater (If your game will be based underwater) and increasing the blur the more the camera goes underwater.

You could probably optimize this by waiting for the Camera.CFrame to change too.

Your idea is good but i was planning to do something more simple, like the second answer of this topic, got any idea of how to get an instance from a table? I know this is really basic but i don’t make the minor idea of how to do this

Nevermind i find out how, thank you everyone that answered the topic :slight_smile:

I am aware of that, it’s a bad habit of mine to make my solutions look more “complete” so it’s more straightforward in a way.