I would like to spawn parts on top of it (in random positions) and I even have an idea of how to do this, but I don’t want to have to do it manually, I mean, having to do the following:
I already have a code that will make Parts spawn in Workspace, see:
while true do
wait(0.1)
local a = Instance.new("Part",workspace)
a.Position = -- I have to find
a.BrickColor = BrickColor.new("White")
end
And I will also have to do the same thing with PosX, in the end, the code will look like this:
while true do
wait(0.1)
local a = Instance.new("Part",workspace)
a.Position = Vector3.new(math.random(-626,-173),15,math.random(-231,231))
a.BrickColor = BrickColor.new("White")
end
And the result looks like this:
As shown, the result is good, but I would like to do this without having all this work (maybe some calculations will solve it), does anyone know a more practical way to do this?
You could write a function that automatically retrieves the bounds of a given part.
local function Get_Bounds(part)
local size = part.Size;
local pos = part.Position;
local min_x, max_x = pos.X - (size.X/2), pos.X + (size.X/2)
local min_z, max_z = pos.Z - (size.Z/2), pos.Z + (size.Z/2)
return {min_x = min_x; max_x = max_x; min_z = min_z; max_z = max_z}
end
If you realistically only use these calculations once in your code, it’s probably more ideal to just set constants instead of writing a function to retrieve the same values.
To understand the calculation you first need to realize that position of any given part is located exactly in the center of that said part.
Now to find the boundaries of a part on the x axis we need its maximum and minimum length on the X axis. To do this we just take that parts position and subtract by half its size to get the maximum. Since half its side it the distance from the center to the edge. To get the minimum we do the same thing but just negate the Size/2 value, which gives us the edge on the opposite side.
Then we just need to get its maximum and minimum length on the z axis which is done the exact same way as we have done it on the x axis. And there you go you now have the bounding box of your part.