Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

FREE Basic Tower Defence Spawn System

Discussion in 'Made With Unity' started by jerobinson, May 27, 2013.

  1. jerobinson

    jerobinson

    Joined:
    May 4, 2013
    Posts:
    3
    Hey Guys!

    I've just finished making this really simple tower defence styled spawn system, and thought I would upload it here for you to use in your own games.
    The script is in C# and I have heavily commented it so it should be easy for a beginner to understand what's going on.
    The spawn system allows you to spawn an enemy at a random xz value on a circumference, between a minimum and maximum distance from the target.
    It will place enemies onto the surface of terrain so it will not matter if you have hills or mountains around your target.
    Enemies will spawn in waves, increasing by 5 enemies each wave (but this value can easily be adjusted).

    Instructions for use:

    1) Attach "SpawnSystem" onto your tower/target/whatever :)

    2) Plug your enemy prefab into the inspector. (Note that your enemy needs to have a CharacterController component added)

    3) Press play!

    Extra:

    If you are using a terrain, ensure that you check "use terrain" as true. Otherwise this value must be false.

    Ensure that the ground/floor in your game is positioned at 0 on the y axis. (if this is not possible, adjust the "spawn height offset" in the inspector to the required value)

    By increasing the "spawn height offset" to a number like 100, enemies will appear to fall from the sky.

    Here is the script. Feel free to use any part of the code in any of your projects :wink:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpawnSystem : MonoBehaviour {
    5.     //Your enemy character prefab (Must have a character controller component)
    6.     public GameObject enemyPrefab;
    7.     //How high above the ground you want your enemy spawned (0 = ground level)
    8.     public float spawnHeightOffset = 0.0f;
    9.     //The minimum distance from the target your enemies will spawn
    10.     public float spawnRadiusMin = 20.0f;
    11.     //The maximum distance from the target your enemies will spawn
    12.     public float spawnRadiusMax = 30.0f;
    13.     //The amount of time between each spawn
    14.     public float spawnDelay = 0.5f;
    15.     //Are you using a terrain? Script uses this to calculate how high above the enemy needs to be placed on spawn
    16.     public bool useTerrain = true;
    17.    
    18.     //How tall is your enemy character
    19.     private float enemyHeight;
    20.     //What is the current wave
    21.     private int curWave = 1;
    22.     //Is the spawn system currently spawning enemies
    23.     private bool isSpawning = false;
    24.     //The initial maximum number of enemies per wave
    25.     private int maxEnemies = 5;
    26.     //The current enemies in the scene
    27.     private GameObject[] curEnemies;
    28.     //The selected spawn point (Random angle and distance from target)
    29.     private Vector3 spawnPoint;
    30.    
    31.     void Start () {
    32.         //Find what the height of the enemy's CharacterContoller component is
    33.         enemyHeight = enemyPrefab.GetComponent<CharacterController>().height;
    34.         //Begin the first wave
    35.         StartCoroutine(SpawnEnemy());
    36.     }
    37.    
    38.     void Update () {
    39.         //Check if the script is NOT spawning enemies
    40.         if (!isSpawning) {
    41.             //Check how many enemies are still in the scene
    42.             curEnemies = GameObject.FindGameObjectsWithTag("Enemy");
    43.             //When there are no more enemies left, increase the current wave
    44.             if (curEnemies.Length <= 0) {
    45.                 IncreaseWave();
    46.             }
    47.         }
    48.     }
    49.    
    50.     IEnumerator SpawnEnemy () {
    51.         //Tell script that we are currently spawning enemies
    52.         isSpawning = true;
    53.         //Loop until script has spawned the maximum number of enemies for this round
    54.         for (int i = 0; i < maxEnemies; i++) {
    55.             //Delay between spawning
    56.             yield return new WaitForSeconds(spawnDelay);
    57.             //Find a random spawn point
    58.             FindSpawnPoint(Random.Range(spawnRadiusMin, spawnRadiusMax), Random.Range(0, 359));
    59.             //Spawn an enemy at the random spawn point
    60.             Instantiate(enemyPrefab, spawnPoint, Quaternion.identity);
    61.         }
    62.         //Tell script that we are finished spawning enemies for this round
    63.         isSpawning = false;
    64.     }
    65.    
    66.     Vector3 FindSpawnPoint(float spawnRadius, int angleInDegrees) {
    67.         //find a random point on the circumference of a circle, with random radius from target between specified min / max distance
    68.         float pointX = (spawnRadius * Mathf.Cos(angleInDegrees * Mathf.PI / 180.0f)) + transform.position.x;
    69.         float pointZ = (spawnRadius * Mathf.Sin(angleInDegrees * Mathf.PI / 180.0f)) + transform.position.z;
    70.         //Set the spawn point into a Vector3
    71.         spawnPoint = new Vector3(pointX, 0.0f, pointZ);
    72.         //Are we using a terrain?
    73.         if (useTerrain) {
    74.             //Adjust the spawn points y value to account for terrain relief
    75.             spawnPoint.y = Terrain.activeTerrain.SampleHeight(spawnPoint);
    76.         }
    77.         //Add half of the enemies height to the y value (So that his feet are on the ground, not waist deep)
    78.         spawnPoint.y += (enemyHeight / 2) + spawnHeightOffset;
    79.         //Return the final spawn point
    80.         return spawnPoint;
    81.     }
    82.    
    83.     void IncreaseWave () {
    84.         //Increase the current wave by 1
    85.         curWave++;
    86.         //Increase the maximum number of enemies for this wave
    87.         maxEnemies = curWave * 5;
    88.         //Start the next wave
    89.         StartCoroutine(SpawnEnemy());
    90.     }
    91.    
    92.     void OnGUI () {
    93.         //Display the wave number in the top left corner of screen
    94.         GUI.Label(new Rect(5, 5, 100, 30), "Wave " + curWave.ToString());  
    95.     }
    96. }
    Tell me what you think. :)
     
    Last edited: May 28, 2013
  2. JerksOfAmerica

    JerksOfAmerica

    Joined:
    Mar 20, 2013
    Posts:
    18
    check your link, saves as a 1x1 .gif
     
  3. jerobinson

    jerobinson

    Joined:
    May 4, 2013
    Posts:
    3
    Sorry, had some problems trying to set up a download link. Just copy / paste code to use.
     
  4. Mozes

    Mozes

    Joined:
    Apr 22, 2013
    Posts:
    42
    very nice script,
    thank you for sharing :D