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!
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