Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MountDoomTeam · Oct 01, 2012 at 01:45 PM · functionloopnested

Best way to make these tree iterations? Arrays? tags? nested etc

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? alt text

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 );            
     }                
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Owen-Reynolds · Oct 01, 2012 at 02:47 PM

If this is an exercise given to you, it's probably about recursion. If you look, your Lines function is calling itself again, with `depth-1`. So, it's really using "tail recursion" (a useless form used only to give students practice) to loop 10 times.

There's usually about an hour explanation of the "basis step(s)" and how the "induction step" has multiple variables where each call is closer to being finished, and a tree-like picture of the call stack. The concept takes a long time to be comfortable with, and many successful programmers never learn it.

For your problem, think about the Rotate loop. Right at the end, you can say "if this branch isn't long enough, run Rotate three more times. Use the end of the line you just made as the new root, rotated sideways and 0, 120 and 240." It might help to know this is pretty much fractals (because everyone knows fractals are hard.)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MountDoomTeam · Oct 01, 2012 at 09:00 PM 0
Share

Thanks Owen! I was convinced that recursion meant that i could program a loop within a loop and magically use the right words so it would call back on itself in the intended form. So i have to at least do an overall routine, prior to figuring out how it would work in an ad infinitum loop. i am just learning from home, and so you pointed me towards reading some course material, i didnt realise all the types of recursion existed. thanks!!!

in 2 $$anonymous$$utes of reading your answer it was rolling.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Loop a function in c#.HELP!!! 2 Answers

Loop a function 1 Answer

while loop not looping 2 Answers

first function dismiss 0 Answers

Loop a Function a Random Number of Times 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges