• 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 Jaguirre · May 13, 2014 at 06:14 PM · positioning

How to reference the position of an object to one another

I'm doing an L-System algorhithm with Unity, and want it to grow instantiating copies of the same object. By the other hand, each object slowly scales up, too.

I want each object to stay next to the others, but not invading their space volumes. How can i do that?

The code of the objects is as follows:

 void Start ()
     {
     iterations = 0;     

     transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); // The initial scale.
     }


 void Update ()
     {
     // Every copy of the object creates one single clone:

     if (iterations < 90) { iterations++; }

     if (iteraciones == 90)
         {
         Instantiate(object1, new Vector3(0, (transform.localScale.y) + transform.localPosition.y, 0), new Quaternion(0, 0, 0F, 0));


         // This is in fact, the stop condition for instatiating objects.

         iteraciones = 100;
         }

     // Each object scales up during its lifetime:

     transform.localScale += new Vector3(0.0001f, 0.001f, 0.0001f);
     }

I realize that somewhere in the code, i must refresh the position of the copied object to maintain the distance to the 'father's' edge, or to his center position, or somewhat.

Any help is pretty appreciated

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
0

Answer by robertbu · May 13, 2014 at 11:52 PM

Doing an 3D L-System is a cool idea. You are going to need to figure out position and rotation to put objects end to end. Here is a bit of code as a demo. Put it on an empty game object and hit the space bar for each new segment. This is not code related to your script. It is a demo script that you can play with and work to understand, and then apply the concepts to your code:

 #pragma strict
 
 private var maxAngle = 45.0;
 private var currScale = Vector3(0.25,0.5,0.25);
 private var scaleBy = 0.8;
 private var lastPos : Vector3;
 private var lastDir : Vector3; 
 private var objectHeight = 2.0;  // Height of object when scale is (1,1,1)
 
 function Start() {
     lastDir = Quaternion.AngleAxis(Random.Range(-maxAngle, maxAngle), Vector3.forward) * Vector3.up;
     lastPos = transform.position;
 }
 
 function Update() {
     if (Input.GetKeyDown(KeyCode.Space)) {
         var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
         sphere.position = lastPos;
         
         if (scaleBy >= 1.0) 
             sphere.localScale = Vector3(currScale.x, currScale.x, currScale.x);
         else
             sphere.localScale = Vector3(currScale.x / scaleBy, currScale.x / scaleBy, currScale.x / scaleBy);
         
         var v = lastDir;
         v = Quaternion.AngleAxis(Random.Range(-maxAngle, maxAngle), Vector3.forward) * v;
         v = v.normalized * currScale.y * objectHeight;
         var endPos = lastPos + v;
         var cylinder =  GameObject.CreatePrimitive(PrimitiveType.Cylinder).transform;
         cylinder.localScale = currScale;
         cylinder.position = lastPos + v * 0.5;
         cylinder.rotation = Quaternion.FromToRotation(Vector3.up, v);
         
         lastDir = v;
         lastPos = endPos;
         currScale *= scaleBy;
     }
 }
Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI changes when game runs full screen 2 Answers

placing children objects in different places 1 Answer

Animation from actual position instead of animation starting position 0 Answers

Positioning using coordinates? 0 Answers

Is it possible to translate a NavMesh at runtime? 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