- Home /
Help with positioning of procedurally generating branches and then leaves of a tree.
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.
[2]: http://i.imgur.com/KbAOd.png
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 - ins$$anonymous$$d 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.
Adjusting 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?
Does 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?
Answer by Owen-Reynolds · Feb 28, 2012 at 04:41 AM
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)
.
Interesting, I will attempt to modify appropriately to use local space axises.
I will change my EulerAngle's over to Rotate because your listed reputation appears solid. However, why exactly do we not want to use EulerAngle's?
Things like transform.right
are really useful for all sorts of positioning. Think of is as switching to localAxis to move something around.
Simple reason for not using 1 euler axis: the docs say not to.
The real reason is that everything is Quaternions and converted to/from xyz angles, and there's more than one way. It's a little like someone who says either 2:45pm or "15 til 3." If you just look at the hours, it will appear to shake between 2 and 3.