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 bobmcdonal · Oct 06, 2020 at 10:18 AM · limitenemy spawnspawning-enemies

How can I make sure my GameObjects don't spawn on top of each other, and that only a certain number remain in the scene at one time?

,This is my first attempt at a game and it's a pretty simple one. I've got a ball that you can control around the scene and enemies that spawn every couple of seconds. So far I can control my character and I can spawn the enemies within the scene at random positions. However they spawn on top of each other, I'm also trying to find a way to limit the number of enemies in the scene, so for example once 10 enemies get spawned the first one to spawn get's despawned, followed by the second etc. So the maximum number of enemies in the scene at one time is 10.

My Spawnerscript so far, the enemies have got box colliders:

 public class EnemySpawnerScript : MonoBehaviour
 {
     public GameObject enemy;
     public GameObject enemy2;
     public GameObject enemy3;
     public GameObject[] enemyArray;
     float randX;
     float randY;
     Vector2 whereToSpawn;
     public float spawnRate = 2f;
     float nextSpawn = 0.0f;
     int RandomOption;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
         if (Time.time > nextSpawn)
         {
             nextSpawn = Time.time + spawnRate;
             //Random Postion
             randX = UnityEngine.Random.Range(-2.5f, 2.5f);
             randY = UnityEngine.Random.Range(-4.8f, 4.8f);
             whereToSpawn = new Vector2(randX, randY);
             //Random enemyObject sprite
             GameObject[] enemyArray = { enemy, enemy2, enemy3 };
             RandomOption = UnityEngine.Random.Range(0, enemyArray.Length);
             //Instantiate
             Instantiate(enemyArray[RandomOption], whereToSpawn, Quaternion.identity);
         }
     }
 }

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

Answer by Klarzahs · Oct 06, 2020 at 04:12 PM

Hi @bobmcdonal

Not spawning ontop:
You can either check that the new random position isn't close to already existing positions:

 Vector3.Distance(enemy1.transform.position, enemy2.transform.position) < ENEMY SIZE

Or you can delete all colliding enemies. For that, have a look at this tutorial, as it is a bit more complex.

Maximum enemies:
Use a list, where you store all your created enemies. If you want to create a new one, check if the list contains the max amount. If so, get the first one and simply reset it to the new position (you might have to reset ammunition, hitpoints etc)

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

136 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

Related Questions

How do I add multiple different enemies to a wave spawner? 2 Answers

How to spawn objects but set a cap of how many can be alive at the same time? 0 Answers

Enemy Spawn System with time and limit enemy in game 2 Answers

c# problem with limiting rotation (with rigidbody) 1 Answer

I cannot store data into PlayerPrefs after a certain amount of data 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