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 haiderInUnity · Dec 31, 2014 at 07:07 PM · 2drandomspawnoverlap

How To avoid random spawn to spawn at previously spawned object position so that they do not overlap

This code randomly spawns objects and then destroy them after some wait. what I want to do is to make sure the random spawned object is not spawned at previously spawned object such as it spawns above it. How may I do that?

 public IEnumerator Spawn () {
             yield return new WaitForSeconds (2.0f);
             counting = true;
             while (timeLeft > 0 && gameOver==false)
             {
                 GameObject ball = balls [Random.Range (0, balls.Length)];
     
                 Vector3 spawnPosition = new Vector3 ( Random.Range (-maxWidth, maxWidth), Random.Range (-maxHeight, maxHeight), 0.0f);
     
                 Quaternion spawnRotation = Quaternion.identity;
     
                 GameObject ballClone = (GameObject) Instantiate (ball, spawnPosition, spawnRotation);
                 StartCoroutine (DestroyAfterWait (ballClone));
                 yield return new WaitForSeconds (Random.Range (1.0f, 2.0f));
     
             }
             //yield return new WaitForSeconds (2.0f);
             GameOver ();
     
         
         }
     

Comment
Add comment · Show 2
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 haiderInUnity · Dec 31, 2014 at 12:22 PM 0
Share

ok thankyou i will read on it and see...

avatar image haiderInUnity · Jan 01, 2015 at 03:16 PM 0
Share

ok i think what i need is Physics2D.OverlapCircle. But i have not been able to figure out how to use it.

here is what i could gather which i add before instantiating ballClone

             Collider[] hitColliders = Physics2D.OverlapCircle(new Vector2(Screen.width/2,Screen.height/2),1000.0f);
 
             foreach (Collider col in colliders)    
             {
 // have to check here if the next spawned object is going to collide with any of the previous ones... 
 //if so then dont  spawn there
             }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by netvortex_dc · Jan 01, 2015 at 04:26 PM

Put the Balls in a List and check if the spawnPosition is closer to it than the size of the ball.

using System.Collections.Generic; private List spawnedBalls = new List ();

 public IEnumerator Spawn () {
 yield return new WaitForSeconds (2.0f);
 counting = true;
 while (timeLeft > 0 && gameOver==false)
 {
 
 MakeClean();
 Vector spawnPosition = GetSpawnPosition();
 
 if(spawnPosition != Vector3.zero)
 {
     Quaternion spawnRotation = Quaternion.identity;
     GameObject ballClone = (GameObject) Instantiate (ball, spawnPosition, spawnRotation);
     spawnedPositions.Add(ballClone);
 
     StartCoroutine (DestroyAfterWait (ballClone));
 }
 
 yield return new WaitForSeconds (Random.Range (1.0f, 2.0f));
 }
 //yield return new WaitForSeconds (2.0f);
 GameOver ();
 }
 
 
 void MakeClean()
 {
     while(!CleanUpList())
     {
         CleanUpList();
     }
 }
 
 bool CleanUpList()
 {
     foreach(GameObject ball in spawnedBalls)
     {
         if(ball != null)
             continue;
         else
         {
             spawnedBalls.Remove(ball);
             return false;
         }
     }
     return true;
 }
 
 Vector3 GetSpawnPosition()
 {
     GameObject ball = balls [Random.Range (0, balls.Length)];
     Vector3 spawnPosition = new Vector3 ( Random.Range (-maxWidth, maxWidth), Random.Range (-maxHeight, maxHeight), 0.0f);
     
     float sizeOfBall = 1.0f;
     foreach(GameObject ball in spawnedBalls)
     {
         if(Vector3.Distance(ball.transform.position, spawnPosition) <= sizeOfBall)
         {
             return Vector3.zero;
         }
     }
     
     return spawnPosition;
 }

Code is untested but i hope you get the idea. I noticed comparing Vectors is much faster than using Physics in large projects so i started to use this approach more often in my projects. (That was in Unity4, not sure about U5). So GetSpawnPosition either returns Vector3.zero or if the position was fine a valid Vector3.

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 haiderInUnity · Jan 01, 2015 at 05:20 PM 0
Share

ok thank you i have read your answer and will work on it to see if it works and then come back.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiate Random 3D Objects/platforms that dont overlap 1 Answer

avoid overlap problem when randomly instantiating 0 Answers

Why do objects keep spawning on top of one another? 0 Answers

Randomly Place Cubes In Viewport Without Overlap 1 Answer

move gameobject into a random position and the spawn an enemy 1 Answer


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