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 /
This question was closed Apr 27, 2016 at 07:44 AM by Internetman.
avatar image
0
Question by Internetman · Apr 25, 2016 at 10:39 AM · c#instantiaterandomspawnspawning problems

Spawn enemies so they aren't spawned 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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Apr 25, 2016 at 10:49 AM

Use a set of positions and an index that you push to avoid using the same entry again.

 public Transform [] spawns;
 private int index = 0;
 public Vector3 GetPosition(){
     if(++index >= this.spawns.Length){
           index = 0;
     }
     return spawns[index];
 }

You could add some more check to make sure the previous guy is gone from the position. The amount of spawns should be logical based on how you move your guys and how often you spawn them but this is the basic idea.

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

Follow this Question

Answers Answers and Comments

134 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

Related Questions

Random element from the list 1 Answer

Distribute terrain in zones 3 Answers

How to randomly spawn three non repeating gameobjects from an array? 2 Answers

Spawn a power up after asteroid death 1 Answer

Don't allow prefab to spawn if prefab already exists at the spawn location. 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