Hi there, i am learning nested functions in javascript and Unity3d.
I am stuck on this task:
rotate a root object 3 times,
add 10 cubes in a line to the root object,
mark the 10th cube as a new root object,
rotate the new root object 3 times,
add 30 cubes to the new root object same as before.
I am very stuck on this task:
how to i mark the 3 new cubes at the end of each branch as a sub root, seeing as they wont have existed before the rotate function was made in the first place? do i tag them and find them at the end of the first run of lines?
Should i make an array with “newroot [1,2,3]”
where do i place the nested elements of the function?
here is the code for the 3 branches.:
#pragma strict
@script RequireComponent(AudioSource)
var cubePrefab : GameObject ;//set cube for model
var startObj : GameObject ;
startObj = gameObject ;//set root object of function
private var rot : Quaternion; //set empty rotation var
rot.eulerAngles = Vector3(0, 0, 0);
function Start()//running this triggers all sub functions
{
Rotate (startObj,rot);
}
function Rotate (ting:GameObject,trot:Quaternion)//rotate the root object 3 times
{
for (var x = 0; x < 3; x++)
{
trot.eulerAngles = Vector3(0,120,0);
ting.transform.rotation = ting.transform.rotation * trot;//take base point and add TROT degrees every loop
Lines( ting, 1.0 , 10.0);//add 10 cubes in a line to the root object
}
}
function Lines( iter:GameObject, ratio:float , depth:float)//add line of cubes
{
var cube = Instantiate(cubePrefab,Vector3(0, 0, 0),rot);
var turn = Vector3(0, 4, 3);
rot.eulerAngles = turn;
cube.transform.position = iter.transform.TransformPoint(Vector3(1, 0, 0));//places new cube on top of last one's relative position
cube.transform.localScale = Vector3(2,2,2);
cube.transform.localScale *= ratio;
//cube.transform.localScale *=.90;//sets size of old cube to new one
cube.transform.rotation = iter.transform.rotation * rot;
//print (iter.transform.rotation);
if(depth > 0)
{
Lines( cube, ratio *.98 , depth -1 );
}
}