Hey guys
Recently Ive been trying to make a tunnel generate with turns and curves randomly. I have the models, but for the prototype im using parts.
FYI I have been scripting since 2 years but I quit 4 months ago and hence have forgetten most of it. Help me revive my memory!
This is the code
local count = 0
local lastPart = workspace.Part
local tunnels = game.ServerStorage.Folder:GetChildren()
local rotationOffset = Vector3.new(0,0,0)
while count < 50 do
count += 1
local p = tunnels[math.random(1, #tunnels)]:Clone()
if p:FindFirstChild("C") and p.C.Value == true then
rotationOffset += Vector3.new(0, 90, 0)
elseif p:FindFirstChild("C") and p.C.Value == false then
rotationOffset += Vector3.new(0, -90, 0)
end
local offset = Vector3.new(lastPart.Size.X/2 + p.Size.X/2, 0, 0)
if rotationOffset.Y ~= 0 then
p.Position = lastPart.Position + Vector3.new(0, 0, offset.X)
else
p.Position = lastPart.Position + offset
end
p.Orientation += rotationOffset
lastPart = p
p.Parent = workspace
print(rotationOffset)
task.wait(1)
end
How do I fix it? As I said earlier if i just get a bit of explaination i might be able to do it easily, i just need someone to click my head due to my past experience.
It looks like youâre putting âoffsetXâ into the Z? Also seems there are not enough ifs to determine if itâs rotated left or right.
Suggestion- take out the ârandomâ and hard-code a short sequence of straights and corners in a table. Physically move the parts where the script should put them and look at each of their numeric transforms, then compare to what your scriptâs doing.
Here, I just subtracted 1 and 3 from the offset and it works fine, turning should be improved though.
local count = 0
local lastPart = workspace.Part
local tunnels = game.ServerStorage.Folder:GetChildren()
local rotationOffset = Vector3.new(0,0,0)
while count < 50 do
count += 1
local p = tunnels[math.random(1, #tunnels)]:Clone()
if p:FindFirstChild("C") and p.C.Value == true then
rotationOffset += Vector3.new(0, 90, 0)
elseif p:FindFirstChild("C") and p.C.Value == false then
rotationOffset += Vector3.new(0, -90, 0)
end
local offset = Vector3.new(lastPart.Size.X/2 + p.Size.X/2, 0, 0)
if rotationOffset.Y ~= 0 then
p.Position = lastPart.Position + Vector3.new(offset.X - 1, 0, offset.X - 3)
else
p.Position = lastPart.Position + offset
end
p.Orientation += rotationOffset
lastPart = p
p.Parent = workspace
print(rotationOffset)
task.wait(1)
end
If I was making this I might just leave rotation out of it and make 6 tiles (or 8 if the straights are directional) and place them on a square grid in world space. Then based on which model you just put down and the next direction you know the next model.
You still have to decide what happens when the tunnel randomly turns back and runs into itself.
Thanks @onboquo for the suggestions, but since the tunnels actual model is curved it might leave gaps between. I will definitely try out your suggestions tho!
@temporaryacount101 did u just randomly subtract 1 and 3 and it worked? If possible please explain me why it works, i want to learn instead of just making it work.