• 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 RaulG · Feb 01, 2015 at 12:35 AM · instantiatetriangles

Instantiating objects to form a triangle shape?

 IEnumerator Live()
     {
         while (true) 
         {
             float posY = 0.0f;
             float posX = 2.0f;
             Vector3 negXY = new Vector3(this.transform.position.x,
                                         this.transform.position.y,
                                         0.0f);
             Vector3 posXY = new Vector3(this.transform.position.x,
                                         this.transform.position.y,
                                         0.0f);
 
             for (int i = 0; i < 1; ++i) 
             {
                 Vector3 objectPosition = Vector3.Lerp(negXY, posXY, (float)i * (1.0f/Points));
                 GameObject bCloneEP = Instantiate(Bullet, new Vector3(objectPosition.x,
                                                                       objectPosition.y - posY,
                                                                       objectPosition.z),
                                                   Quaternion.identity) as GameObject;
                 posY -= 4;
                 posX = -4;
 
                 for (int ii = 0; ii < 3; ++ii) 
                 {
                     Vector3 objectPosition2 = Vector3.Lerp(negXY, posXY, (float)ii * (1.0f/Points));
                     GameObject bCloneEP2 = Instantiate(Bullet, new Vector3(objectPosition.x + posX,
                                                                            objectPosition.y - posY,
                                                                            objectPosition.z),
                                                       Quaternion.identity) as GameObject;
 
                     posX += 4.0f;
                     if( ii == 3)
                     {
                         posX = -8.0f;
                     }
                 }
 
                 for (int iii = 0; iii < 5; ++iii) 
                 {
                     posY = -8.0f;
                     Vector3 objectPosition2 = Vector3.Lerp(negXY, posXY, (float)iii * (1.0f/Points));
                     GameObject bCloneEP2 = Instantiate(Bullet, new Vector3(objectPosition.x + posX,
                                                                            objectPosition.y - posY,
                                                                            objectPosition.z),
                                                        Quaternion.identity) as GameObject;
                     
                     posX -= 4.0f;
                 }
             }


Alright, well that works. But it's painful to look at. I'll edit it as I go along to make it done through a single loop instead of through multiple loops.

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
2

Answer by nurikabe · Feb 01, 2015 at 01:23 AM

A for loop that tests floating point equality or inequality can easily become an infinite loop due to floating point imprecision.

For each line segment do a loop up to the count of the numbers of objects that must be instantiated per line segment (only counting one of the end two points). Calculate the position of each object by using Vector3.Lerp in between the two end points. Something like:

 for (int i = 0; i < objectsPerSegment; ++i) {
    Vector3 objectPosition = Vector3.Lerp(segStartPos, segEndPos, (float)i * (1f/objectsPerSegment);
    // instantiate the object at that position
 }
Comment
Add comment · Show 16 · 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 RaulG · Feb 01, 2015 at 01:28 AM 0
Share

Whoa never seen that before. Ill check it out in a moment.

avatar image RaulG · Feb 01, 2015 at 01:45 AM 0
Share

So would I have to make a new for loop per line?

avatar image nurikabe · Feb 02, 2015 at 06:09 PM 0
Share

Yes. For a triangle that will be 3 loops.

avatar image RaulG · Feb 02, 2015 at 06:21 PM 0
Share

Wont that be insanely taxing on the computer?

avatar image nurikabe · Feb 02, 2015 at 06:53 PM 0
Share

The cost of the for loops and the lerp calculations is insignificant compared to the cost of instantiating the objects.

Show more comments

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

Checking if object intersects? 1 Answer

Issues with instantiated prefabs 1 Answer

Instancing an object on my character 1 Answer

How do you Instantiate a button as a child object of a canvas? 0 Answers

snake game tail problems 0 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