Detect if a camera is in a region 3

How do you detect if a players camera is in a region3?

1 Like

check if the y is under the highest pos and y is over the lowest pos at the same time, and then you can check it for the other x and z

local Camera = workspace.CurrentCamera

local function CameraIsInRegion(Region)
	return Camera.CFrame.Y > Region.CFrame.Y + Region.Size.Y / 2 and Camera.CFrame.Y < Region.CFrame.Y - Region.Size.Y / 2 and Camera.CFrame.X > Region.CFrame.X + Region.Size.X / 2 and Camera.CFrame.X < Region.CFrame.X - Region.Size.X / 2 and Camera.CFrame.Z > Region.CFrame.Z + Region.Size.Z / 2 and Camera.CFrame.Z < Region.CFrame.Z - Region.Size.Z / 2
end

It doesn’t seem to work for me, here is my code.

local player_camera = game.Workspace.CurrentCamera	
local Camera = player_camera

while wait(1) do
	for i, v in pairs(workspace:GetDescendants()) do
		if v.Name == "indoor" then
			local pos1 = v.Position - (v.Size / 2)
			local pos2 = v.Position + (v.Size / 2)
			local Region = Region3.new(pos1, pos2)
			if Camera.CFrame.Y > Region.CFrame.Y + Region.Size.Y / 2 and Camera.CFrame.Y < Region.CFrame.Y - Region.Size.Y / 2 and Camera.CFrame.X > Region.CFrame.X + Region.Size.X / 2 and Camera.CFrame.X < Region.CFrame.X - Region.Size.X / 2 and Camera.CFrame.Z > Region.CFrame.Z + Region.Size.Z / 2 and Camera.CFrame.Z < Region.CFrame.Z - Region.Size.Z / 2 then
				game.Workspace.SoundGroup.Volume = 0.1
			end
		end
	end
end

Still doesn’t work for some reason

swap pos1 and pos2 instead

local player_camera = game.Workspace.CurrentCamera	
local Camera = player_camera

while wait(1) do
	for i, v in pairs(workspace:GetDescendants()) do
		if v.Name == "indoor" then
			local pos1 = v.Position - (v.Size / 2)
			local pos2 = v.Position + (v.Size / 2)
			local Region = Region3.new(pos2, pos1)
			if Camera.CFrame.Y > Region.CFrame.Y + Region.Size.Y / 2 and Camera.CFrame.Y < Region.CFrame.Y - Region.Size.Y / 2 and Camera.CFrame.X > Region.CFrame.X + Region.Size.X / 2 and Camera.CFrame.X < Region.CFrame.X - Region.Size.X / 2 and Camera.CFrame.Z > Region.CFrame.Z + Region.Size.Z / 2 and Camera.CFrame.Z < Region.CFrame.Z - Region.Size.Z / 2 then
				game.Workspace.SoundGroup.Volume = 0.1
			end
		end
	end
end
1 Like

Wow, thanks it works. I even made it so if the camera isn’t in the region3, the volume goes back to normal. Thanks for the help.

1 Like

It only works in one Region 3 spot, is there a way this can happen with multiple blocks?

you can double it like this
if CameraIsInRegion(Region1) and CameraIsInRegion(Region2) then

what about a table? since im using this detection thing for trains, and trains are constantly spawning and despawning

nevermind, i figured it out, i just made a table and a function that went through the table

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.