Hi, im triying to make a code that generates a city,with roads districts and such, i’ve managed to already get the road gen working and stuff but i cant really seem to figure out how to generate buildings besides the road without them clipping into one another like this:
and this:
i’ve tried checking the angle difference between road intersections and try to reduce the amoun of buildings based on that but it didnt really work very well.
does anyone know any method that could work?
if its of any help this is the code that generates the building(the factor function is what i used to do the angle checking but i removed out of the code for now)
for i,dot:BasePart in pairs(chunk:GetChildren()) do
--gets the X and Y coords of the current position on the grid
--btw, dot is a random point in a gridsquare. and dot is used to connected roads between one another. preety neat.
local x1 = dot.gridpart.dot:GetAttribute("X")
local y1 = dot.gridpart.dot:GetAttribute("Y")
--these for loops make it check both the grid squares with an offset of 1,0 and 0,1 grid squares relative to this one
--and creates an road either named Right, or Down based on that.
for y = 0,1 do
for x = 0,1 do
if x == 0 or y == 0 then
task.wait()
--basically if the gridsquare its attempting to check DOES exist then run the following code
if grid[y + y1] ~= nil then
local chance = math.random(0,100)
if grid[y + y1][x1 + x] ~= nil and chance >= 10 then
--this is cuz the normal dot is actually an folder, the actual dot part with a random position in the grid
--is inside the folder. thats what dot2 is. its the ACTUAL dot basepart.
local dot2 = dot.gridpart.dot
local road = Instance.new("Part")
local distance = (dot2.Position - grid[y + y1][x1 + x].Position).Magnitude
road.Size = Vector3.new(0.1 * gridsize,1,distance + 5)
road.Material = Enum.Material.Concrete
road.Anchored = true
road.Color = Color3.fromRGB(57, 57, 57)
if x == 1 then
road.Name = "Right"
elseif y == 1 then
road.Name = "Down"
end
road.CFrame = CFrame.lookAt(dot2.Position + (CFrame.lookAt(dot2.Position, grid[y + y1][x1 + x].Position).LookVector * (distance / 2)),grid[y + y1][x1 + x].Position)
road.Parent = dot
--old function that checked for the angle difference between grid squares
--the results were too weird honestly and i dont think it fit very well
local factor = findfactor(x1,y1,x,y)
road.Name = factor
--size is basically how many buildings(should in theory) fit on one road.
local size = math.floor(((distance) / 32) - 3)
if size < 0 then
size = 0
end
local buildings = Instance.new("Folder")
buildings.Parent = road
--this generates buildings one both sides of the road, since the for loop is only set to
-- -1,0 it will only generate buildings on the left side of the road and not on the right(makes it easier to debug :P)
for o = -1,0 do
if o ~= 0 then
for i = -size /2 , size / 2 do
local building1 = Instance.new("Part")
building1.Size = Vector3.new(32,math.random(64,128),32)
building1.Anchored = true
building1.Rotation = road.Rotation
building1.Position = road.Position + (road.CFrame.RightVector * ((8 + (0.1 * gridsize)) * o) + road.CFrame.LookVector * ((i) * 32)) + Vector3.new(0,building1.Size.Y / 2,0)
building1.Parent = buildings
building1.Name = "building"
building1:SetAttribute("chunk",x1..y1)
end
end
end
end
end
end
end
end
end