Hello DevForum!
A few days ago I managed to make a working 2D “landscape” generator, and I was wondering how it could be improved, currently it works fine but I already know it can be improved, on a side note, I would like to add more features to it, in which case I will reply with the new code in this thread.
Code:
local part = workspace.Part
local xOffset = 1
local startingHeight = Vector3.new(0,0,0)
local function CreateCubes()
local startingCube = Instance.new("Part")
startingCube.Position = startingHeight
startingCube.Size = Vector3.new(1,1,1)
startingCube.Anchored = true
startingCube.Color = Color3.new(0,0,0)
startingCube.Material = Enum.Material.Neon
startingCube.Parent = workspace
print("starting cube created")
local oldCube = startingCube
while true do
local newCube = Instance.new("Part")
newCube.Position = Vector3.new(xOffset,oldCube.Position.Y + math.random(-1,1),0)
if newCube.Position.Y >= 100 then
newCube.Position = Vector3.new(xOffset,oldCube.Position.Y + math.random(-1,0), 0)
elseif newCube.Position.Y <= -100 then
newCube.Position = Vector3.new(xOffset,oldCube.Position + math.random(0,1),0)
end
if newCube.Position.Y >= -15 and newCube.Position.Y <= 15 then
newCube.Color = Color3.new(0.371252, 0.745892, 0.370169)
newCube.Material = Enum.Material.Grass
elseif newCube.Position.Y >= -30 and newCube.Position.Y <= -15 then
newCube.Color = Color3.new(0.394614, 0.394614, 0.394614)
newCube.Material = Enum.Material.Rock
elseif newCube.Position.Y >= -60 and newCube.Position.Y <= -30 then
newCube.Color = Color3.new(0.203494, 0.203494, 0.203494)
newCube.Material = Enum.Material.Rock
elseif newCube.Position.Y >= -100 and newCube.Position.Y <= -60 then
newCube.Color = Color3.new(0.570199, 0.202441, 0.0988327)
newCube.Material = Enum.Material.CrackedLava
elseif newCube.Position.Y >= 15 and newCube.Position.Y <= 30 then
newCube.Color = Color3.new(0.74905, 0.74905, 0.74905)
newCube.Material = Enum.Material.Rock
elseif newCube.Position.Y >= 30 and newCube.Position.Y <= 60 then
newCube.Color = Color3.new(0.919417, 0.919417, 0.919417)
newCube.Material = Enum.Material.Snow
elseif newCube.Position.Y >= 60 and newCube.Position.Y <= 100 then
newCube.Color = Color3.new(0.638529, 0.827054, 1)
newCube.Material = Enum.Material.Ice
end
newCube.Size = Vector3.new(1,1,1)
newCube.Anchored = true
newCube.Parent = workspace
print("new cube created")
oldCube = newCube
xOffset += 1
wait(1/60)
print("restarting")
end
end
local function DestroyPart()
wait(tick)
part:Destroy()
end
part.Touched:Connect(CreateCubes)
part.Touched:Connect(DestroyPart)
I also noticed a few things with this generator, 1: when I set the rate at 1/120, it actually goes at the same speed, and when I put the wait time to destroy the part to 1, it actually makes a “shotgun” effect.
I also know the method I put to trigger this isn’t the best but it was the only one I could think of.