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 /
avatar image
0
Question by Anas173 · Jun 25, 2016 at 09:49 AM · enemyspawningenemy spawn

Enemy Spawner

Im working on a game and i just cant figure out how to fix my enemy spawner. Im aiming for a certain behavior which is spawning easy enemies x20( or any no.) then medium enemies and after that a endless spawn of random easy, medium and hard enemies . I also want between each spawn to have like a delay or break for 1-2 seconds so it doesn't looks like the enemies are spamming on top of each other

Currently i'm having only easy and medium enemies to spawn and its ending its not endless besides im getting an array out of range error

Your help is appreciated and thank you in advance This is my Code:

 using UnityEngine;
  using System.Collections.Generic;
  using UnityEngine.UI;
 
   public class Test : MonoBehaviour
   {
 
    public GameObject[] enemy; 
 
    public Transform[] spawnPoints;         
 
    private float timer = 2;
 
 
    int index = 0 ;
 
    int wave = 0;
 
    List <GameObject> EnemiesList = new           List<GameObject>();
 
   private int enemyCount=20;
 
 
   void Update()
  {
   timer -= Time.deltaTime;
 
 if (timer <= 0 && wave < 6)
 {
     timer = 3;
 
     if (wave != 0 &&  wave % 2 == 0)
     {
         index ++ ;
     }
 
     EnemySpawner();
 
     wave++;
 }
 
 }
 
  void Spawn ()
  {
 
 for (int i = 0; i<enemyCount;i++)
 {
     Invoke("EnemySpawner" , i + 2);
 }
 }
 
 void EnemySpawner ()
  {
 int spawnPointIndex = Random.Range (0, spawnPoints.Length);
 
 GameObject InstanceEnemies= Instantiate ( enemy[index] , spawnPoints[spawnPointIndex].position , spawnPoints[spawnPointIndex].rotation) as GameObject;
 
 EnemiesList.Add(InstanceEnemies);
 
  }
 
 
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
Best Answer

Answer by Mindmapfreak · Jun 25, 2016 at 10:53 AM

I am not sure what wave and index exactly do in your code. I thought there are only 3 waves (only easy enemies, only medium enemies, random enemy types) so why are you check if wave is less than 6? I would do it like this:

 public class Spawner : MonoBehaviour {
 
     public GameObject EasyEnemey;
     public GameObject MediumEnemey;
     public GameObject HardEnemey;
     public Transform[] SpawnPoints;
     public float TimeBetweenSpawns;
     public int NumberOfEasyEnemiesToSpawn;
     public int NumberOfMediumEnemiesToSpawn;
     public float EasyChance;
     public float MediumChance;
     public float HardChance;
 
 
     private int waveNumber;
     private float spawnTimer;
     private int numberOfEasyEnemies;
     private int numberOfMediumEnemies;
     // Use this for initialization
     void Start()
     {
         this.spawnTimer = 0.0f;
         this.waveNumber = 0;
         float totalChance = this.EasyChance + this.MediumChance + this.HardChance;
         if(Mathf.Abs(totalChance-1.0f)>0.0001f) { 
             Debug.LogWarning("Warning: The chances should add up to 1.0 ("+totalChance+" currently)");
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         this.spawnTimer -= Time.deltaTime;
         if(this.spawnTimer<=0.0f)
         {
             Transform spawnPoint = this.SpawnPoints[Random.Range(0, this.SpawnPoints.Length)];
             Vector3 spawnPos = spawnPoint.position;
             Quaternion spawnRot = spawnPoint.rotation;
             switch(this.waveNumber)
             {
                 case 0:
                     Instantiate(EasyEnemey, spawnPos,spawnRot);
                     this.numberOfEasyEnemies++;
                     if(this.numberOfEasyEnemies>=this.NumberOfEasyEnemiesToSpawn)
                     {
                         this.waveNumber++;
                     }
                     break;
                 case 1:
                     Instantiate(MediumEnemey, spawnPos, spawnRot);
                     this.numberOfMediumEnemies++;
                     if (this.numberOfMediumEnemies >= this.NumberOfMediumEnemiesToSpawn)
                     {
                         this.waveNumber++;
                     }
                     break;
                 case 2:
                     float randomFloat = Random.value;
                     if(randomFloat<this.EasyChance)
                     {
                         Instantiate(EasyEnemey, spawnPos, spawnRot);
                     }
                     else if(randomFloat<this.EasyChance+this.MediumChance)
                     {
                         Instantiate(MediumEnemey, spawnPos, spawnRot);
                     }
                     else
                     {
                         Instantiate(HardEnemey, spawnPos, spawnRot);
                     }
                     break;
             }
             this.spawnTimer = this.TimeBetweenSpawns;
         }
 
     }
 
 }
Comment
Add comment · Show 5 · 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 Anas173 · Jun 25, 2016 at 12:23 PM 0
Share

Hello, Can you explain the totalchance logic? i really dont get it.

Thank you.

avatar image Mindmapfreak Anas173 · Jun 25, 2016 at 12:45 PM 0
Share

If you want to spawn 50% easy enemies, 30% medium enemies and 20% hard enemies in the last wave, you have to set EasyChance to 0.5, $$anonymous$$ediumChance to 0.3 and HardChance to 0.2. The chances should naturally add up to 1.0. Random.value gives you a float between 0 and 1. The if-else-conditions in this example basically are (pseudocode)

 if(randomFloat between 0 and 0.5) {//0.5 out of 1.0 = 50%
     spawnEasyEnemy();
 }
 else if(randomFloat between 0.5 and 0.8) {//0.3 out of 1.0 = 30%
     spawn$$anonymous$$ediumEnemy();
 }
 else if(randomFloat between 0.8 and 1) {//0.2 out of 1.0 = 20%
     spawnHardEnemy();
 }

EDIT: Or do you mean the condition in Start()? That one just checks if totalChance differs from 1.0 and factors in floating point precision.

avatar image Anas173 · Jun 25, 2016 at 12:38 PM 0
Share

i had in the inspector to spawn 5 easy and 5 medium it spawns 5 easy and 5 medium then it keeps spawning easy forever. it's never spawning medium or hard again.

avatar image Mindmapfreak Anas173 · Jun 25, 2016 at 12:46 PM 0
Share

What values did you set EasyChance, $$anonymous$$ediumChance and HardChance to?

avatar image Anas173 Mindmapfreak · Jun 25, 2016 at 01:02 PM 0
Share

Thank you very much. this is working perfectly. And i understand everything now.

Thanks Again :)

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

59 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

Related Questions

When enemy spawns it loses the script settings. 0 Answers

How can I change scene or Quit game after killing all the spawned enemies? 1 Answer

Shooting enemy to spawn them 0 Answers

My GameObject remains inGame but is destroyed in the editor (no GameObject on left side/in hierarchy) 0 Answers

How to make all enemies attack after spawning? Not only 1. 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