How to check whether or not a CFrame falls under a Region

I want to check whether or not a CFrame is within a region or not.

I tried asking some friends and looking up devforum posts, but didn’t find anything.

I just can’t think of a way to make it work. Region3 has a function called “FindPartsInRegion” but if I were to create a part just to check if a CFrame is within a region then it would make the game very laggy. Any sort of help would be very appreciated!

1 Like

I could be wrong or I’m just misunderstanding you, but:

CFrame , short for coordinate frame , is a data type that describes a 3D position and orientation.

You cant find a cframe within a region because its a data type and not a physical object like a basepart is.

That’s the problem, I specified that FindPartsInRegion() won’t work for CFrames and I was looking for an alternative in the topic.

Im quite sure this is not the best but:
Insert a part and put it to that cframe
See if the part is inside

I already thought of that possibility, it would be very laggy.

Okay but why are you trying to find a cframe within a region?

Can you convert it into a vector3 then go from there?

You could use Region3 by @EgoMoose to do this,
To do it with CFrames you can check X and Y position of the part and put a limitation

As an example with “Positions”

if game.Workspace.ExamplePart.Y > -10 then
-- Do stuff
end

By the way, why would you do it like this? Use “touched” function

As you know, cameras only use CFrames, so I want to confirm whether or not a camera is within a region or not.

Depending on if your correctly optimize it and if you arent checking every single frame, it wouldn’t be as laggy as you think.

Edit: There’s probably an API reference that does it better than what I’m suggesting.

Ah of course, this was giving me heavy XY problem vibes.

There’s plenty of posts asking the same question.

Maybe perhaps you could do something with world to screen point?

you can create one part and put it to the camera’s cframe whenever the camera’s cframe is changed and it wouldnt be that laggy, thats what I do here
Underwater Effects - Resources / Community Resources - DevForum | Roblox

A bit late but it is actually much easier than it seems.

local Size = Vector3.new(5,1,5) -- Size of Hitbox You can change it to whatever you want
local cframe = CFrame.new(0,10,0) -- Position of Hitbox again you can make it whatever you want

local regionMaxLocal = cframe * CFrame.new(Size.X/2,0,Size.Z/2) -- Gets the max corner cframe of your hitbox
local regionMinLocal = cframe * CFrame.new(-Size.X/2,0,-Size.Z/2) -- Gets the min corner cframe of your hitbox

local MinPart = Instance.new("Part",workspace)
MinPart.Anchored = true
MinPart.Size = Vector3.new(1,1,1)
MinPart.CFrame = regionMinLocal -- visual representation for min corner cframe this part is unecessary and can be left out

local MaxPart = Instance.new("Part",workspace)
MaxPart.Anchored = true
MaxPart.Size = Vector3.new(1,1,1)
MaxPart.CFrame = regionMaxLocal -- visual representation for max corner cframe this part is unecessary and can be left out

-- Example CFrame to check

local Point = CFrame.new(Vector3.new(0, 10, 5)) -- The Cframe that you want to check is in a region. Can be your enemy's hrp

local Part = Instance.new("Part",workspace)
Part.Anchored = true
Part.Size = Vector3.new(1,1,1)
Part.CFrame = Point -- visual representation for point cframe this part is unecessary and can be left out

local position = Point.Position -- position of Point to check

local regionMinTransformed = regionMinLocal:pointToObjectSpace(position) -- getting the cframe of the point relative to the max corner point
local regionMaxTransformed = regionMaxLocal:pointToObjectSpace(position) -- getting the cframe of the point relative to the min corner point

-- Check if the position is within the transformed region
if regionMinTransformed.X >= 0 and regionMaxTransformed.X <= 0 and regionMinTransformed.Z >= 0 and regionMaxTransformed.Z <= 0 and regionMinTransformed.Y >= 0 and regionMaxTransformed.Y <= 0 then
	print("The CFrame is within the specified region.")
else
	print("The CFrame is outside the specified region.")
end

Basically you want to check for the maximum and minimum corner points for your hitbox and then get the cframe of the position you want to check relative to each point. For the max corner point you want to make sure that the relative cframe is not more than it and for the minimum point you want to check if it is not less than it. The reason you cant just simply check the x,y,z values with >/< is because it would not work with rotations and it would be messed up. hopefully this helps even after 3 years

1 Like