Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jadde_dahlberg · Sep 05, 2018 at 06:21 PM · positionvector3instantiate prefabforloopcolission

Instantiate a 2D world question

Hello! I am trying to instantiate a bunch of prefabs on the X-axis and have done so without much trouble using a simple forloop code.

However my current problem is that the objects that I am instantiating at random varies in size. So sometimes what happens since the whole spawning of the objects is at random. Is that they spawn inside eachother. This makes it extremely difficult at times to actually play the game.

I am wondering if there is a better way of instantiating objects in a line where they wont be instantiated inside eachother.

The code I'm currently using is quite simple being:

         for (int x = 0; x < worldSize; x++)
         {
             Vector3 pos = new Vector3(Random.Range(400, 800) * x , Random.Range(-300, -450), 0);
             Instantiate(WorldObjects[Random.Range(0, WorldObjects.Length -1)], pos, Quaternion.identity, 
             this.transform);
         }


I've been fiddling around with the vector3 position but it doesnt quite solve my problem since it's quite random.

Thanks for your help! :)

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

2 Replies

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

Answer by toddisarockstar · Sep 06, 2018 at 12:22 AM

if you want items touching in a line the math is simple.the distance between two objects should be half of the the first item's width plus half the touching items width.

here is an example:

     void Start () {
 
         float x = 0;
         float randomsize = 0;
         int i = 10;while(i>0){i--;
              
             x=x+randomsize*.5f;//adding half of the previos item's size;
             randomsize = Random.Range(2f,6f);
             x=x+randomsize*.5f;//now adding half of the current item's size;
             GameObject g = GameObject.CreatePrimitive(PrimitiveType.Cube);
 
             g.transform.renderer.material.color = new Color(Random.Range(0f,1f),Random.Range(0f,1f),Random.Range(0f,1f),1);
             g.transform.localScale = new Vector3(randomsize,randomsize,randomsize);
             g.transform.position = new Vector3(x,0,0);
 
             }
 
     }


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

Answer by tormentoarmagedoom · Sep 05, 2018 at 06:43 PM

Good day.

The solution can be very simple.

You only need to check the distance between the position (pos) and all instantiated objects. You only need to have an array or list with all the instantiated objects (or you add them to a list when instantiate them, or you make them have the same tag)

Imagine all instantiated objects have the tag "Obj"

Then only need to check the distance betwee the position you are going to use and all the objects taged as "Obj". If some of the distances is lower than a value (for example 100), recalculate the position you want. You will need to create a method for cheking this.

      for (int x = 0; x < worldSize; x++)
      {
          Vector3 pos = new Vector3(Random.Range(400, 800) * x , Random.Range(-300, -450), 0);

        while (ObjectTooClose(pos) == true)
       {
          pos = new Vector3(Random.Range(400, 800) * x , Random.Range(-300, -450), 0);
        }

          Instantiate(WorldObjects[Random.Range(0, WorldObjects.Length -1)], pos, Quaternion.identity, 
          this.transform);
      }

[...]

 public bool ObjectTooClose(Vector3 PositionToCheck)
 {
 foreach (GameObject OneObject in GameObject.FindObjectsWithTag("Obj")
 {
   if (Vector3.Distance (OneObject.transform.position, PositionToCheck) <100)
     {
     return true;
     }
 }
 return false;
 }

Sooooo, it checks for all objects distance to the position you want to use. If find some object too close, it returns true, so the random position is calculated again. But if does not find any object too close, it can continue and instantiate a new object.

This method have a risk, if you spawn too much objects, maybe can not find any position away from other objects, and the while loop will be infinite...

But this is the idea, (Maybe the code have some error, so check it, understand what is doing and solve!)

Bye!

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 jadde_dahlberg · Sep 05, 2018 at 06:46 PM 0
Share

Thank you! That's a very detailed and specific explanation and I think I understand what you're getting at :) I'll give it a shot and see if it can help me!

Thanks again for the quick reply ^^

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

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

Related Questions

Instantiating objects at position 1 Answer

C# Transform modification. 1 Answer

How to stop transform movement at a certain point? 1 Answer

Any help with prevent camera from passing a specific coordinate? 1 Answer

How do i make an object move unpredictably? 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