Hi, I am making a spawning module that can spawn in models and parts in an area and I can’t find a way to clamp a part to the boundaries of the area. The parts are randomly rotated (0-360) and randomly positioned.
This image shows what I want to achieve, I want the parts to be within the red border. (I took the image in an orthographic view)
I was going to try and get the size of the part in world size and use that to get the offset between the border and the part position but I am not that good with CFrames and I couldn’t find a solution to this problem on another topic, but I might have missed one or something idk.
This is a 3d version of the first one (With another seed)
You could have the generated parts made invisible upon creation. To check if they are in bounds, you could create an invisible wall positioned on each side of the generated area and check for collisions. Then make the generated parts visible once they are approved and if they aren’t approved, they can be destroyed.
local function CheckIfOutOfBounds(part)
local PartIsOutOfBounds = false
if table.find(workspace:GetPartsInPart(OutOfBoundsSide1), part) or table.find(workspace:GetPartsInPart(OutOfBoundsSide2), part) or table.find(workspace:GetPartsInPart(OutOfBoundsSide3), part) or table.find(workspace:GetPartsInPart(OutOfBoundsSide4), part) then
PartIsOutOfBounds = true
end
return PartIsOutOfBounds
end
--↓ Part Generation ↓--
local GeneratedPart = Instance.new("Part", PartLocation)
GeneratedPart.Transparency = 1
--Change Size, Position, CFrame, Color, etc.
if CheckIfOutOfBounds(GeneratedPart) == false then
GeneratedPart.Transparency = 0
else
GeneratedPart:Destroy()
end
Though this solution probably isn’t very ideal since it’s done after creation and requires destroying parts (taxing on server performance) rather than comparing CFrames before creation. It really depends on how often you are generating parts.
Hi good idea! My system already uses this mechanic to make sure the parts doesn’t spawn in eachother and I could use that to check if its within the part or not. Though I don’t know how I would check if a randomly rotated part is 100% within the area.