I’m working on something, where i need to spawn about 30-40 parts over a surface.
These parts need to be evenly spread out.
I tried using this function to get a random x and z coordinate, however
the parts spawned unevenly. Some were very close to each other while some were too far away from each other.
local function GetRandomXZ(Ground)
return Ground.Position.X + math.random(-Ground.Size.X/2,Ground.Size.X/2),Ground.Position.Z + math.random(-Ground.Size.Z/2,Ground.Size.Z/2)
end
Any solutions to make these 30-40 parts spawn evenly?
(by evenly, i mean that i want the parts to spawn in such a way that they are equally distributed on the surface. They are neither concentrated in one region, nor missing in the other)
not exactly a grid.
If you play pet simulator x, you will notice that the breakable objects like coins and chests are evenly spread out on the ground. This is what I’m trying to achieve.
suppose i have 30 parts,
how do i get 30 positions, on the surface of another part, which are evenly spread out? @Boustifail
local length = 20
local center= CenterPart
local cf = center.CFrame
local rng = math.random
for i = 1, 30 do
local part = YourPart
local x, y, z = rng(-center.Size.X/2, center.Size.X/2), rng(0, 5), rng(-center.Size.Z/2, center.Size.Z/2)
part.CFrame = cf * CFrame.new(x, y + center.Size.Y/2, z)
end
You can pre-fill a table with possible positions and remove a random one. Then remake the table if it is empty. That will prevent most cases of picking the same values in close succession.
Check out “Poisson-disc distribution” if you want to ensure the positions are not on a grid.
In general, if you want something evenly spread out, you cannot use random. Random numbers will sometimes cluster, that is part of the randomness.
does not solve the problem of the parts being unevenly distributed.
the parts are concentrated in one region, and the other region barely has any parts.