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 GoodArcade · Aug 15, 2014 at 06:51 PM · instantiateobjectrandomoverlap

How can i randomly create an array of objects without colliding one another?

Hello again! I am trying to create a script that could come up with a number of randomly generated positions to place objects. How can i avoid having the objects collide with each other? I know how to DETECT a collision (using Physics.OverlapSphere or CheckSphere) but how would i apply this to this script?

This is what I know:

 var count:int = 5;
 var prefab:Transform;
 
     ///This will create an array of objects in random positions.
     for(var i = 0; i < count; i++)
     {
             var randPos = Vector3(Random.Range(-10, 10), Random.Range(-10, 10), Random.Range(-10, 10));
         newObj = Instantiate(prefab, transform.position, transform.rotation);
     }

And just checking and repositioning the object isnt ideal either because it may never find a spot in the for-loop, which could crash unity. Anyone have any other approaches?

Comment
Add comment · Show 1
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 jokim · Aug 15, 2014 at 06:58 PM 0
Share

Do you have access to the size of the object ? if not, using gameObject.renderer.bounds.size gives you a vector 3 representing its size.

You could spawn them so you know they won't touch each other. ever.

EDIT- wait, I forgot about the random in this.

You could translate objects manually when they're in collision, until they are no more. The first point is totally random, if it hits something, then you push it aside until it's free.

1 Reply

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

Answer by zharik86 · Aug 15, 2014 at 07:10 PM

If you want use Physics.OverlapSphere and you objects have unique tag, for example, "mob", that here small example(write on CSharp):

  public Transform myPrefab = null; //reference for your object
  public float myRadius = 2.0f; //Radius of the sphere described round object
  public int myCount = 5; //count created objects

  void Awake() {
   Vector3 randPos = Vector3.zero;
   int myCheck = 0; //count overlap colliders
   for(int i = 0; i < myCount; i++) {
    do {
     myCheck = 0;
     randPos = Vector3(Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f));
     Collider[] hitColliders = Physics.OverlapSphere(ramdPos, myRadius);
     for(int j = 0; j < hitColliders.Length; j++) {
      if (hitColliders[j].tag == "mob") {
       myCheck++;
      }
     }
    } while (myCheck > 0);
    Transform newObj = (Transform) Instantiate(prefab, randPos, Quaternion.identity);  
   }
  }

But at the wrong initial parameters at you the infinite cycle can study. Be attentive. I hope that it will help you.

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 GoodArcade · Aug 16, 2014 at 03:01 AM 0
Share

I figured it out. I ended up with just about the same solution. Ive tweeked it a bit to avoid infinite cycles by adding an aborting function. Thanks bunches! :)

avatar image Avash · Feb 06, 2016 at 02:28 PM 0
Share

Had to fix some bugs but thanks.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Spawn GameObjects without overlap 1 Answer

One of the numbers as Second ? 1 Answer

Instantiate object in random position inside a navmesh 1 Answer

Spawning different random objects at the same position? 2 Answers

Random array issue C# 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