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
1
Question by Ecnalyr · Feb 23, 2012 at 10:12 PM · transform.positionprocedural generationlocalposition

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):

alt text

Grow:

alt text

Into this object:

alt text

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

After it grows a little:

alt text

And finally this: alt text

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

Comment
Add comment · Show 3
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 Ecnalyr · Feb 24, 2012 at 01:53 AM 0
Share

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.

avatar image Ecnalyr · Feb 24, 2012 at 07:28 PM 0
Share

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?

avatar image Ecnalyr · Feb 28, 2012 at 03:17 AM 0
Share

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?

1 Reply

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

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).

Comment
Add comment · Show 2 · 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 Ecnalyr · Feb 28, 2012 at 05:41 AM 0
Share

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?

avatar image Owen-Reynolds · Feb 28, 2012 at 10:44 PM 0
Share

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.

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

6 People are following this question.

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

Related Questions

Is there a way to lock the local position of a game object? 0 Answers

Positions of Objects in Unity - World Position, Transform Position, Inspector Position, Local Position, Prefab Positions, Center of Objects for Parents & Childs 1 Answer

World coordinates conversion to cell coordinates in tilemap 1 Answer

How to get max and min points of a game object on the x and z axis? 3 Answers

How to have an object some distance above another object Locally? 1 Answer


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