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 VedSarkar · Mar 31, 2016 at 09:55 PM · rigidbodycolliderspawning

Avoid overlapping of random spawned prefab

I used a code to spawn 2 different prefabs .The script is working but sometimes the objects get's overlapped ,and I can't figure out how to avoid this overlapping.

     void SpawnSpike()
     {
         //the bottom point of the screen
         Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0));
         
         //this is the top-right point of the screen
         Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));
         
         //instantiate an enemy1
         GameObject anSpikesGO = (GameObject)Instantiate (SpikesGO);
 
         anSpikesGO.transform.position = new Vector2 (Random.Range (-min.x, -max.x), -max.y);
         
         //Schedule when to spawn next enemy
         ScheduleNextSpikeSpawn ();
     }
     void ScheduleNextSpikeSpawn()
     {
         float spawnInSeconds;
         
         if (maxSpawnRateInSeconds > 2f) 
         {
             //pick a number between 1 to max
             spawnInSeconds = Random.Range (2f, maxSpawnRateInSeconds);
         } 
         else
             spawnInSeconds = 2f;
         Invoke ("SpawnSpike", spawnInSeconds);
     }
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 Cherno · Mar 31, 2016 at 11:01 PM 0
Share

Cast Physics.OverlapBox or similar shape before instantiation, using the object's renderer.bounds as the shape dimensions and casting against the layer the other already existing objects are in.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Oshroth · Apr 01, 2016 at 12:41 AM

Use Physics2D to check if the spawn position is occupied and keep checking random positions until you find a blank space.

 int attempts = 0;
         do {
             // Get a Random spawn Position

             if(!Physics2D.OverlapArea(topLeft, BottomRight)) { // Check the bounds of the spawn position
                 break;
             }
             Debug.Log("Collision during spawn at: " + temp);
         } while (++attempts <= 10); // Limit spawn attempts to prevent infinite loop
         if(attempts > 10) {
             Debug.Log("Failed to find a clear spawn point");
         }
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 FlightOfOne · Mar 31, 2016 at 10:55 PM

There's several ways to accomplish this but I think the easiest would be to use a collider or trigger and using tags. If it collided with an object with the same take, re-position it again using random range, as you have done.

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 Lancemaker_ · Jul 19, 2016 at 01:52 PM

use raycast using the object witht/hight divided / 2 as the max distance on -x, +x,-y,+y directions. if it returns anything repeat until you find a proper spot.

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

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character Controller Component Makes the Player Pass through Hills, Rocks, Trees, etc! 0 Answers

GameObject sometimes collides, but sometimes goes through each other. 0 Answers

col.gameObject.layer is not working 1 Answer

Whats the difference between a rigid body and a collider? 2 Answers

Avoid one gameObject skin in another gameObject 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