Ecnalyr
1
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.
What works better for me is to forget localPosition. Childing automatically figures local position for you. Instead, to make a leaf on some branch B with an arbitrary facing, use local space axises: B.up*2+B.right*0.2. That moves you up along the branch and “branch right” of it, in local branch coords.
Also, things like EulerAngle.z aren’t recommended. To get branch B2 to angle a little local left of branch B1, can try B2.rotation = B1.rotation; B2.Rotate(0,0,10).