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 /
  • Help Room /
This question was closed May 24, 2016 at 12:11 PM by Internetman.
avatar image
0
Question by Internetman · Apr 27, 2016 at 08:19 AM · c#instantiaterandomspawnspawning problems

Spawn enemies so they aren't instantiated on top of each other (C#)

Hello my fellow great Unites! I have an enemy spawn system that spawns out enemies using Ienumerator inside a set of borders. Now the only problem is that the enemies will sometimes spawn out on the player, or another object...

How can I fix this problem? Using a for loop?

Here's the code:

  using UnityEngine;
  using System.Collections;
  using System.Collections.Generic;
  
  public class EnemySpawn : MonoBehaviour 
  {
      // EnemyPrefabs
      public GameObject EnemyBouncingPrefab;
      public GameObject EnemyGrowerPrefab;
      public GameObject EnemyMinePrefab;
  
      //Show where spawn is prefab
      public GameObject showEnemySpawn;
      public GameObject showEnemyFlyBySpawn;
      public GameObject showEnemyGrowerSpawn;
      public GameObject showEnemyMineSpawn;
  
  
      // Array of spawn points 
      public Transform[] spawnPointsEnemyFollow; 
      public Transform[] spawnPointsEnemyExplode;
      public Transform[] spawnPointsEnemyFlyBy;
      
      // Borders
      public Transform borderTop;
      public Transform borderBottom;
      public Transform borderLeft;
      public Transform borderRight;
      
  
      //Used to put active enemies in a list
      public static List<GameObject> activeEnemies;
  
      void Start () 
      {
          // Starts a coroutine function for spawning bouncing enemies and fly by enemy
          StartCoroutine(SpawnBouncingEnemy());
          StartCoroutine(SpawnGrowerEnemy());
          StartCoroutine(SpawnEnemyFlyBy());
          StartCoroutine(SpawnMineEnemy());
      }
  
      void Awake()
      {
          // Create the list
          activeEnemies = new List<GameObject>();
  
      }
      
      // Spawn a Bouncing enemy
      IEnumerator SpawnBouncingEnemy() 
      {
          //Wait 3 to 5 seconds when game starts to spawn a ball
          yield return new WaitForSeconds(Random.Range(1, 3));
  
          while(true)
          {
              //Calls the function to set random position
              Vector2 spawnPoint = RandomPointWithinBorders();
              
              // Show spawn location for one second
              Object marker = Instantiate(showEnemySpawn, spawnPoint, Quaternion.identity);
              yield return new WaitForSeconds(1);
              Destroy(marker);
              
              // Spawn enemy
              GameObject newEnemy = (GameObject) Instantiate(EnemyBouncingPrefab, spawnPoint, Quaternion.identity);
  
              activeEnemies.Add(newEnemy);
  
              yield return new WaitForSeconds(Random.Range(4, 6));
  
          }
          
      }    
  
      // Spawn a Grower enemy
      IEnumerator SpawnGrowerEnemy() 
      {
          //Wait 3 to 5 seconds when game starts to spawn a "grower"
          yield return new WaitForSeconds(Random.Range(20, 40));
          
          while(true)
          {
              //Calls the function to set random position
              Vector2 spawnPoint = RandomPointWithinBorders();
              
              // Show spawn location for one second
              Object marker = Instantiate(showEnemyGrowerSpawn, spawnPoint, Quaternion.identity);
              yield return new WaitForSeconds(1);
              Destroy(marker);
              
              // Spawn enemy
              GameObject newEnemy = (GameObject) Instantiate(EnemyGrowerPrefab, spawnPoint, Quaternion.identity);
              
              activeEnemies.Add(newEnemy);
              
              yield return new WaitForSeconds(Random.Range(20, 50));
              
          }
          
      }
  
      // Spawn a Mine enemy
      IEnumerator SpawnMineEnemy() 
      {
          //Wait 40 to 60 seconds when game starts to spawn a "Mine_enemy"
          yield return new WaitForSeconds(Random.Range(45, 60));
          
          while(true)
          {
              //Calls the function to set random position
              Vector2 spawnPoint = RandomPointWithinBorders();
              
              // Show spawn location for one second
              Object marker = Instantiate(showEnemyMineSpawn, spawnPoint, Quaternion.identity);
              yield return new WaitForSeconds(2);
              Destroy(marker);
              
              // Spawn enemy
              GameObject newEnemy = (GameObject) Instantiate(EnemyMinePrefab, spawnPoint, Quaternion.identity);
              
              activeEnemies.Add(newEnemy);
              
              yield return new WaitForSeconds(Random.Range(50, 60));
              
          }
          
      }
  
      public Vector2 RandomPointWithinBorders()
      {
          //Code that will spawn a bouncing ball and ShowSpawn at a random position inside the borders 
          Vector2 random = new Vector2();
          random.x = (int)Random.Range(borderLeft.position.x, borderRight.position.x);
          random.y = (int)Random.Range(borderBottom.position.y, borderTop.position.y);
          return random;
      }
  }
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

2 Replies

  • Sort: 
avatar image
0

Answer by Nimred · Apr 27, 2016 at 10:09 AM

The usual way to solve this problem is to set up multiple spawn points in your level. You place them manually in the editor, mark them in a special way (a tag, or more likely you write up a "SpawnPoint" component), then when spawning your objects you keep track of which spawn point has been used, and spawn the object at an unused spawn point's location.

In order to reuse the spawn points, you could also give them a simple box collider, set up as a trigger. This way once you spawn an object at this location, the spawned object enters the trigger; once the spawned object exits the trigger, you know you can use the spawn point again.

You might also want to have an enum value on the SpawnPoint component to decide if it's a player spawn point, an AI spawn point, 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
avatar image
0

Answer by meat5000 · Apr 27, 2016 at 10:09 AM

Use Physics.OverlapSphere to check the space before Instantiate.

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 Internetman · Apr 27, 2016 at 11:26 AM 0
Share

I've seen this solution before but I'm not quite sure where to implement it

avatar image meat5000 ♦ Internetman · Apr 28, 2016 at 07:19 AM 0
Share

You implement it before the Instantiate at spawnpoint. So if there's nothing in the collider[] you fire the Instantiate. You will probably need a layermask to select the layers you want to use in your detection.

Follow this Question

Answers Answers and Comments

137 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 avatar image

Related Questions

Trying to make a field of meteors forever, but I can tell I'm doing it horribly 0 Answers

Spawn gameObject horde, modify concentration of spawned objects. 1 Answer

Problem with Instantiated clones 1 Answer

[c#] Increase speed of instantiating over time!? 1 Answer

Spawn Random Object script spawns too much 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