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 giangvubinhn · Jun 11, 2020 at 06:04 AM · arraypositionobjectrandom

Position an array of objects randomly

Hi guy, with the code below, I am able to load all assets from the asset bundle as an array and import them to the scene at run time, However, they all still spawn in the same position, which is (0,0,0). Now I want to spawn them at random positions that's at least 10 meters from each other, I've tried some for loops and stuffs, but they did not work out well, so I'm not sure how to go about this. Any tip would help, thank you!

 IEnumerator DownloadAndLoadModels()
     {
         //Link to the host server
         string url = "link to the server";
 
         UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
         yield return request.SendWebRequest();
 
         AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
 
         //Load all assets to the engine
         GameObject[] allAssets = bundle.LoadAllAssets<GameObject>();
         amountsOfAsset = new GameObject[allAssets.Length];
         for (int i = 0; i < allAssets.Length; i++)
         {
             amountsOfAsset[i] = Instantiate(allAssets[i]) as GameObject;
         }
     }
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
Best Answer

Answer by ShadyProductions · Jun 11, 2020 at 07:19 AM

      var random = new Random();
      const int min = 10;
      const int max = 100;

      int previousX = 0;
      int previousY = 0;
      int previousZ = 0;

      for (int i = 0; i < allAssets.Length; i++)
      {
          var newX = previousX + random.Next(min, max+1);
          var newY = previousY + random.Next(min, max+1);
          var newZ = previousZ + random.Next(min, max+1); //remove this if 2D

          amountsOfAsset[i] = Instantiate(allAssets[i]) as GameObject;
          amountsOfAsset[i].transform.position = new Vector3(newX, newY, newZ);

          previousX = newX;
          previousY = newY;
          previousZ = newZ;
      }

I thought this was the more simple part :) Note that this one only goes in positive coordinates starting from 0,0,0

What you can also do is grab a random position and then check all other spawn positions you already have and do a distance check to see if its >= 10 but that depends more on randomness so it can be less performant.

 const int min = -500;
 const int max = 500;
 List<Vector3> spawnPositions = new List<Vector3>(amountsOfAsset.Length);
 foreach (asset in amountsOfAsset) 
 {
     Vector3 pos = new Vector3(random.Next(min,max), random.Next(min,max), random.Next(min,max));
     while (spawnPositions.Any(a => Vector3.Distance(a, pos) < 10))
         pos = new Vector3(random.Next(min,max), random.Next(min,max), random.Next(min,max));
     spawnPositions.Add(pos);
     asset.transform.position = pos;
 }

Something like this.. Note that you have to compensate with min and max based on how many assets you have. And may want to add some check to prevent infinite loop in while statement in case it cannot find a suitable position (there is a chance that it cannot find a suitable position in the while loop if u have many assets and little min/max space).

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 giangvubinhn · Jun 11, 2020 at 03:36 PM 0
Share

Thank you so much for your effort. This helps alot!!!

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

176 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 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

moving random objects to random positions 1 Answer

How do I change the position of an object on the screen randomly ? 0 Answers

What's the best way to check if an area is empty? 7 Answers

Spawn object at a random predeterminated(transform) position 1 Answer

Randomly instantiate objects from array without choosing the same item twice. 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