Hello,
Is there any way for me to control a tree through script? Can I start it small, scale it up over time, add and remove branches and leaves, one at a time?
Where would one start? I looked at the Tree class, and it seems to pull data from tree.data, but it says to “refer to the editor”. Does that mean the editor is the only way to get in there?
As ChrisD mentions, modeling/animation is probably the best non-programmatic solution. I could see one solution to have several “states” for the tree, say for instance Sapling, Young, Mature. For each state you have a base tree modeled then in Unity add several “branch” points. In your script you can then use these points to (randomly? selectively?) parent a branch mesh to these point(s).
So break up the tree into separate meshes for the trunk and branches, use Empty GameObject’s as mount points to mount your branches (could even randomize the rotations here a little bit for extra variation) and mount the branches to those Empty GameObjects when you want to add branches.
The same approach can be used for leaves, add mount points again along the branch and parent leaves to them (or leaf clusters), etc…
Try something like this:
-
Create two Cube primitives: the first is 1.5f tall and say 0.25f wide and deep (like a staff standing on end), the second, 4.5f tall and say 0.45f wide and deep. The first is your “sapling” and the second a “mature” tree. Rename them both from Cube and Cube1 to Sapling and Mature.
-
Next, on the bigger “mature” tree, create an Empty GameObject and in the hierarchy, click and drag it on top of Mature. Rename this Empty GameObject to BranchMountPoint and position it halfway up the tree.
-
Now create another Empty GameObject and call it “Branch”, then create a cube primitive and stretch it out horizontally this time, drag it over Branch and set it up so that Branch is at one end of the new cube primitive. You should be able to drag Branch around and see the new cube primitive move with it.
-
At this point you can just setup a “public Transform branchMount;” declaration in a script and attach it to the tree, then assign branchMount to be the BranchMountPoint transform you created in step 2. Also, add “public Transform branch;” to easily reference your branch (you can figure out details of tying this together yourself using a DBMS or XML configuration or whatever).
-
Finally in your script, do something like:
void Start( )
{
branch.parent = branchMount;
branch.rotation = Quaternion.identity;
branch.position = Vector3.zero;
}
Should attach the branch to your tree - the rest is just using this and scaling it to multiple mount points, etc…
Awesome answers, thanks guys.
What I’m thinking is making a simple(?) simulation for a plant’s life. I would like to start at germination, and slowly grow into a full sized plant.
I have no experience in modeling whatsoever, so I have no idea if that would be a good idea or not. I would lean towards not, since I’m already learning Unity for school.
I would like to programmatically “grow” the tree based on resource collection. A player would ideally collect sunlight, carbon, and water in order to grow; I’m planning on starting with water to keep it simple.
So are you saying I can attach a “branch” to any type of game object?
I also noticed that a tree is made from a series of vertical control points of some kind. Is there a way for me to add those to an existing tree?
And the all important question: How?
-Fred
Ok! I’ve got the code in there and am just waiting for some free time to dig into it.
Thanks again for your help!