How would I make a tree generator?

How would I go about creating a tree generator?

1 Like

Depends, are you wanting to create an actual tree? Or create a tree that slowly generates branch paths from random directions?

A tree that generates random branch paths

There is a plugin you can use, but it’s actually not the best plugin. I have used it before.

not with a plugin, like periodically spawning trees in

If you want to periodically spawn trees, use a loop.

local maxSpawnTime = 20
local spawnTime = math.random(1, maxSpawnTime)
local tree = -- your tree path
local baseplateSize = --your baseplate or map width here

while wait(spawnTime) do

     local newTree = tree:Clone()
     newTree.Position = Vector3.new(math.random(-baseplateSize/2, baseplateSize/2), 0, math.random(-baseplateSize/2, baseplateSize/2)) -- you can adjust this Vector3 value to your liking
     newTree.Parent = game.Workspace 

     spawnTime =  math.random(1, maxSpawnTime)
end

*Written in comment editor: Hope this helps!

Ah thanks but I’m having more problems with positioning the forks in the trees

Do you want it so when it spawns it creates random forks?

Yes as in randomly generated tree

1 Like

Untitled
Like how would I get that position for the forks

I think you could use to objectworldspace to get the relative Y axis of the part or just use CFrame.

You could make a folder in replicated Storage with a bunch of different trees, then choose a random one.

local maxSpawnTime = 20
local spawnTime = math.random(1, maxSpawnTime)
local trees = -- your tree folder path
local baseplateSize = --your baseplate or map width here
local allTrees = trees:GetChildren()

while wait(spawnTime) do
     local newTree = allTrees[math.random(1, #allTrees)]:Clone()
     newTree.Position = Vector3.new(math.random(-baseplateSize/2, baseplateSize/2), 0, math.random(-baseplateSize/2, baseplateSize/2)) -- you can adjust this Vector3 value to your liking
     newTree.Parent = game.Workspace 

     spawnTime =  math.random(1, maxSpawnTime)
end

*Written in comment editor: Hope this helps!

I’m trying to achieve randomly generated trees not a selection from a set of premade trees

I have an idea, you could start with the base for the tree, then a ray is cast in some direction, you create a branch the size of the ray with its position being the origin / the limit , do the same for the forks but with a smaller size

How would I make a part the size of a ray?

using ray.magnitude :shallow_pan_of_food:

1 Like

Maybe you could get the length of the ray and set one or two of the Vector3 size values to the length.

@ThanksRoBama has already made a palm tree generator, what you will find useful is how the parts are made an attached to the tree. For something else other than a palm tree you will have come up with your own.

Edit: CCing @DaMajestick, this should help with the CFrames

ah ok thanks I will try using rays

It would have the prerequisite of the front side being the one with the branches’ point, although you could circumvent that buy using rightvector or upvector.