I am creating a tree using this code:
#pragma strict
var branch1 : Transform;
var prefab : Transform;
function Start () {
yield WaitForSeconds(1);
var branch2 : Transform;
var branch3 : Transform;
branch2 = Instantiate(prefab);
branch3 = Instantiate(prefab);
branch2.transform.parent = transform;
branch2.localPosition = (transform.localPosition + Vector3(-1,1,0));
branch3.transform.parent = transform;
branch3.localPosition = (transform.localPosition + Vector3(1,1,0));
branch2.eulerAngles.z = branch1.eulerAngles.z+45;
branch3.eulerAngles.z = branch1.eulerAngles.z-45;
}
This makes this object (which is the same as the ‘prefab’ in the code):

Grow:

Into this object:

This almost looks like what I want it to (a tree), but I am wanting to add leaves to it.
When I attempt to add leaves using this code:
#pragma strict
var branch1 : Transform;
var prefab : Transform;
var leaf : Transform;
function Start () {
yield WaitForSeconds(1);
var branch2 : Transform;
var branch3 : Transform;
branch2 = Instantiate(prefab);
branch3 = Instantiate(prefab);
branch2.transform.parent = transform;
branch2.localPosition = (transform.localPosition + Vector3(-1,1,0));
branch3.transform.parent = transform;
branch3.localPosition = (transform.localPosition + Vector3(1,1,0));
branch2.eulerAngles.z = branch1.eulerAngles.z+45;
branch3.eulerAngles.z = branch1.eulerAngles.z-45;
var leaf1 : Transform;
var leaf2 : Transform;
var leaf3 : Transform;
leaf1 = Instantiate(leaf);
leaf1.transform.parent = transform;
leaf1.localPosition = (transform.localPosition + Vector3(-.3,2.4, 0));
leaf2 = Instantiate(leaf);
leaf2.transform.parent = transform;
leaf2.localPosition = (transform.localPosition + Vector3(-.3,.8, 0));
leaf3 = Instantiate(leaf);
leaf3.transform.parent = transform;
leaf3.localPosition = (transform.localPosition + Vector3(.3,1.6, 0));
}
Same start phase:

After it grows a little:

And finally this:

As you can see, the ‘leaves’ do not spawn on the original branch. I want the ‘leaves’ to spawn adjacent to the existing branch (making it look like they are ‘attached’ to the branch. They are not doing this.
My ultimate goal is to grow branches based off of an initial branch that will look like a tree (got something close to that now), and make it so each branch can grow three leaves at fixed locations attached to the parent branch.
Any advice on how to approach this situation would be appreciated.
The leaves appear to be getting assign a position after the original branch is given the two additional branches as children. When this happens, the 'center' of the branch is no longer the location where the leaves are drawn - instead they are drawn based on the center of the entire parent-child system, which includes two additional branches, and places the leaves too far away from the branches.
– anon46463775Adjusting the localPosition of the leaves allows me to move them to a more reasonable position, but this appears to be an ineffective workaround. I want the leaves to be 'attached' directly to just the branch they are supposed to be attached to. Anyone have any tips on how to clean up my code to arrive at that situation?
– anon46463775Does anyone know how to add children objects to a parent object without moving the gameobject's overall position (while the new children object's position is not exactly that of the parent gameobject?
– anon46463775