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 ben9902 · Apr 25, 2017 at 02:33 PM · enemiesspawner

How do I make the enemies spawn with increasing difficulty?

Hi guys!

I am experimenting with one tutorial games provided by Unity: Survival Shooter https://unity3d.com/learn/tutorials/projects/survival-shooter-tutorial and I am trying to get the enemies to spawn with increasing difficulty, e.g when all enemies are dead, the next wave will spawn one more enemies than the last.

I have implemented Corrupter heart's enemy spawner successfully however am unable to figure out how to solve this issue.

The script I am using is below:

 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
 // Filename: Spawner.cs
 //  
 // Author: Garth "Corrupted Heart" de Wet <mydeathofme[at]gmail[dot]com>
 // Website: www.CorruptedHeart.co.cc
 // 
 // Copyright (c) 2010 Garth "Corrupted Heart" de Wet
 //  
 // Permission is hereby granted, free of charge (a donation is welcome at my website), to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to deal
 // in the Software without restriction, including without limitation the rights
 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 // copies of the Software, and to permit persons to whom the Software is
 // furnished to do so, subject to the following conditions:
 // 
 // The above copyright notice and this permission notice shall be included in
 // all copies or substantial portions of the Software.
 // 
 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 // THE SOFTWARE.
 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Spawner : MonoBehaviour
 {
     // Color of the gizmo
     public Color gizmoColor = Color.red;
 
     //-----------------------------------
     // All the Enums
     //-----------------------------------
     // Spawn types
     public enum SpawnTypes
     {
         //Normal,
         //Once,
         //Wave,
         TimedWave
     }
     // The different Enemy levels
     public enum EnemyLevels
     {
         Easy,
         //Medium,
         Hard,
         //Boss
     }
     //---------------------------------
     // End of the Enums
     //---------------------------------
 
     // Enemy level to be spawnedEnemy
     public EnemyLevels enemyLevel = EnemyLevels.Easy;
 
     //----------------------------------
     // Enemy Prefabs
     //----------------------------------
     public GameObject EasyEnemy;
     //public GameObject MediumEnemy;
     public GameObject HardEnemy;
     //public GameObject BossEnemy;
     private Dictionary<EnemyLevels, GameObject> Enemies = new Dictionary<EnemyLevels, GameObject>(4);
   
     //----------------------------------
     // End of Enemy Prefabs
     //----------------------------------
 
     //----------------------------------
     // Enemies and how many have been created and how many are to be created
     //----------------------------------
     public int totalEnemy = 10;
     private int numEnemy = 0;
     private int spawnedEnemy = 0;
     public PlayerHealth playerHealth;
     public Transform[] spawnPoints;
     //----------------------------------
     // End of Enemy Settings
     //----------------------------------
 
 
     // The ID of the spawner
     private int SpawnID;
 
     //----------------------------------
     // Different Spawn states and ways of doing them
     //----------------------------------
     private bool waveSpawn = false;
     public bool Spawn = true;
     public SpawnTypes spawnType = SpawnTypes.TimedWave;
     // timed wave controls
     public float waveTimer = 30.0f;
     private float timeTillWave = 0.0f;
     //Wave controls
     public int totalWaves = 5;
     private int numWaves = 0;
     //----------------------------------
     // End of Different Spawn states and ways of doing them
     //----------------------------------
     void Start()
     {
         // sets a random number for the id of the spawner
         SpawnID = Random.Range(1, 500);
         Enemies.Add(EnemyLevels.Easy, EasyEnemy);
         //Enemies.Add(EnemyLevels.Boss, BossEnemy);
         //Enemies.Add(EnemyLevels.Medium, MediumEnemy);
         Enemies.Add(EnemyLevels.Hard, HardEnemy);
     }
     // Draws a cube to show where the spawn point is... Useful if you don't have a object that show the spawn point
     void OnDrawGizmos()
     {
         // Sets the color to red
         Gizmos.color = gizmoColor;
         //draws a small cube at the location of the gam object that the script is attached to
         Gizmos.DrawCube(transform.position, new Vector3(0.5f, 0.5f, 0.5f));
     }
     void Update()
     {
         if (Spawn)
         {
 
             // Spawns enemies in waves but based on time.
             if (spawnType == SpawnTypes.TimedWave)
             {
                 // checks if the number of waves is bigger than the total waves
                 if (numWaves <= totalWaves)
                 {
                     // Increases the timer to allow the timed waves to work
                     timeTillWave += Time.deltaTime;
                     if (waveSpawn)
                     {
                         //spawns an enemy
                         spawnEnemy();
                     }
                     // checks if the time is equal to the time required for a new wave
                     if (timeTillWave >= waveTimer)
                     {
                         // enables the wave spawner
                         waveSpawn = true;
                         // sets the time back to zero
                         timeTillWave = 0.0f;
                         // increases the number of waves
                         numWaves++;
                         // A hack to get it to spawn the same number of enemies regardless of how many have been killed
                         numEnemy = 0;
                     }
                     if (numEnemy >= totalEnemy)
                     {
                         // diables the wave spawner
                         waveSpawn = false;
                     }
                 }
                 else
                 {
                     Spawn = false;
                 }
             }
         }
     }
     // spawns an enemy based on the enemy level that you selected
     private void spawnEnemy()
     {
         // Find a random index between zero and one less than the number of spawn points.
         int spawnPointIndex = Random.Range(0, spawnPoints.Length);
         // Spawns Enemies in the positions the spawn points have been placed
         GameObject Enemy = (GameObject)Instantiate(Enemies[enemyLevel], spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
         // Increase the total number of enemies spawned and the number of spawned enemies
         numEnemy++;
         spawnedEnemy++;
 
         // If the player has no health left...
         if (playerHealth.currentHealth <= 0f)
         {
             // ... exit the function.
             return;
         }
     }
     // Call this function from the enemy when it "dies" to remove an enemy count
     public void killEnemy(int sID)
     {
         // if the enemy's spawnId is equal to this spawnersID then remove an enemy count
         if (SpawnID == sID)
         {
             numEnemy--;
         }
     }
     //enable the spawner based on spawnerID
     public void enableSpawner(int sID)
     {
         if (SpawnID == sID)
         {
             Spawn = true;
         }
     }
     //disable the spawner based on spawnerID
     public void disableSpawner(int sID)
     {
         if (SpawnID == sID)
         {
             Spawn = false;
         }
     }
     // returns the Time Till the Next Wave, for a interface, ect.
     public float TimeTillWave
     {
         get
         {
             return timeTillWave;
         }
     }
     // Enable the spawner, useful for trigger events because you don't know the spawner's ID.
     public void enableTrigger()
     {
         Spawn = true;
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

99 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

Related Questions

2D Game Kit Enemies 0 Answers

I want to make a Monster Spawner that spawns a monster in every x seconds (in real time); but I can't get timer to work properly... 1 Answer

Trouble With Creating A Collection of Enemy Prefabs and Spawning Them 0 Answers

Resetting the level without changing variables 0 Answers

Multiple enemies and no animations 0 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