• 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 SavvyK · May 07, 2015 at 02:12 PM · instantiation

Instantiating prefabs overlap regardless of Physics.CheckSphere

I've got a randomizer that selects a location on a quad and then spawns an "enemy" prefab. The problem is, no matter what I do, the prefabs inevitably overlap once or twice. Sometimes they are nearly directly on top of one another. I can't see why my use of CheckSphere is not preventing the randomizer from resetting if the location is occupied. And yes, I have swapped out the ! beforehand.

Any insight would be greatly appreciated.

 {
     public int maxEnemies = 5;
     public static int enemyCount = 0;
     public float spawnRate = 2f;
 
     public static SpawnEnemies instance = null;
     public GameObject enemy;
     public Vector2 mapPosition;
     public Vector2 randomSpawn;
 
     public float timer = 0.5f;
     public float stamp;
 
     void Start()
     {
         mapPosition = new Vector2(0f,0f);
         stamp = Time.time;
         randomSpawn = mapPosition + new Vector2(Random.Range(-5f, 5f), Random.Range(-5f, 5f));
         for (int i = 0; i < maxEnemies; i++)
         {
             MakeEnemy();
         }
             Spawn();
     }
 
     void MakeEnemy()
     {
         Instantiate(enemy, randomSpawn, Quaternion.identity);
     }
 
     public void Spawn()
     {
         if (enemyCount <= maxEnemies)
         {
             if (Time.time >= stamp)
             {
                 MakeEnemy();
                 enemyCount += 1;
                 stamp = Time.time + timer;
             }
         }
     }
 
 
     void Randomizer()
     {
         randomSpawn = mapPosition + new Vector2(Random.Range(-5f, 5f), Random.Range(-5f, 5f));
     }
 
     void Update()
     {
         Randomizer();
         while(!Physics.CheckSphere(randomSpawn,1.5f))
         {
             Randomizer();
         }
         if(enemyCount < maxEnemies)
         {
             Spawn();
         }
     }
 }
Comment
Add comment · Show 3
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 SavvyK · May 07, 2015 at 05:39 PM 0
Share

I ended up completely rewriting this class into a serializable one that creates a grid that marks occupied locations...but I am still interested in why the original code was failing.

avatar image Fappp · May 07, 2015 at 08:00 PM 0
Share

Haha, great solution! Don't know why it fails, but why not just test overlapping with colliders? And move left or right the objects size?

avatar image SavvyK · May 07, 2015 at 08:09 PM 0
Share

I was originally using OnCollisionEnter2D(Collision2D col) destroy(gameObject), but that was a resource waste...I don't know of the solution you're talking about but I imagine I'd prefer it because the serialized version looks really rigid. It's too "perfect" looking

1 Reply

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

Answer by SavvyK · May 28, 2015 at 05:19 AM

For anyone in the future who wants to resolve this issue...

 while (Physics2D.OverlapArea(SpawnChecker.randomSpawn - new Vector2(1f, 1f) SpawnChecker.randomSpawn + new Vector2(1f, 1f)) != null)

That line was what fixed the issue I originally posted this question for. I revisited it today and was able to execute a resolution without having to use the grid system I mentioned in a comment above.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiation happens three times, but called only once 1 Answer

Is it necessary to assign the result of Instantiate() to a variable and is there a need to always typecast? 1 Answer

instantiate object and let instantiated object know what it got instantiated from 2 Answers

Setting rotation parallel to ground 1 Answer

scripts gets disabled when the prefab is instantiated 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