Enemies: Movement and Damage Dealing

Enemy AI: Infinite Spawn Point / Tower

You don’t have access to this lesson
Please register or sign in to access the course content.

This lesson assumes that you have completed the guide[5] on how to get an enemy object to chase the player using the nav mesh agent. You can find the lesson here.

There is an example of an infinite enemy spawn tower which can be downloaded here.

This will set an object that will infinitely spawn objects that will chase the player. There will be a delay between each object being spawned.

Create a new prefab (it can just be a cube) to use as the spawn tower.

Add an empty gameobject named SpawnPoint, this will be the position where the spawned objects are created.

Create a script called SpawnTowerManager.

Attach this script to the SpawnTower and open it.

Add the points numbered in the code below

  1. Create a Transform variable[1] to store the spawn point that we want the objects to be created at
  2. Create a public gameobject that will be used to store the prefab object (enemy prefab) that we want to spawn
  3. Create a Transform that will contain the position of the target that the spawned objects should move towards.
  4. This creates an IEnumerator variable which is needed so that we can set spawn delays and have multiple objects continually generated over time.
  5. This float[6] sets the amount of time in seconds that will be between each object being created. It is set to 2 seconds by default.
  6. We create a function[2] called SpawnEnemy. This will spawn the enemies. It takes one parameter[3] / argument, that of the delay or wait time that should pass before spawning another object.
  7. We have an infinite loop[7] as we want this infinite spawner to always run.
    We could replace this with a for loop if we only wanted it to run a set number of times.
  8. The yield return new WaitForSeconds() function pauses this function but allows the rest of the program to run as normal. It will wait until the time passed to WaitForSeconds has passed, in this case[8] two seconds before continuing with the later lines of code in the SpawnEnemy function.
  9. Here we spawn the enemy object at the provided spawn point.
  10. Here we set the target of the enemy to the one stored in the Target variable created in point 3. This assumes that the object we are spawning has a script component[4] attached to it called EnemyTargetPlayer with a public variable targetPosition.
  11. Here we create the SpawnEnemy IEnumerator with the delay.
  12. Now we start the coroutine which will run indefinitely.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnTowerManager : MonoBehaviour
{   
    // 1. Point to spawn the enemy at from this tower
    public Transform SpawnPoint;
    // 2. Prefab of the enemy to spawn from this spawn tower
    public GameObject EnemyToSpawn;

    // 3. The object that the spawned target should aim at.
    public Transform Target;

    // 4. Ienumerator for running a coroutine to keep spawning
    private IEnumerator spawnTimer;

    // 5. The time to wait between spawning enemies from this tower.
    public float spawnDelay = 2.0f;

    // Start is called before the first frame update
    void Start()
    {
        // 11. Run the spawner once
        spawnTimer = SpawnEnemy(spawnDelay);
        // 12. Start the coroutine to repeatedly
        StartCoroutine(spawnTimer);
    }

    // 6. Coroutine function to spawn enemies, will wait until time expires
    private IEnumerator SpawnEnemy(float waitTime)
    {
        // 7. Always run
        while (true)
        {
            // 8. Wait until the wait time has passed before doing anything
            yield return new WaitForSeconds(waitTime);
            // 9. Spawn / Instantiate the enemy at the location given in worldspace.
            GameObject enemyGameObject = Instantiate(EnemyToSpawn, SpawnPoint.position, Quaternion.identity);
            /* 10. 
            Set the new enemies target to the player. 
            Note that this assumes there is a script on the enemy gameobject called
            EnemyTargetPlayer with a public variable targetPosition
            */
            enemyGameObject.GetComponent<EnemyTargetPlayer>().targetPosition = Target;
        }
    }
}

Save the script and place the spawn tower prefab on the scene.

Now we need[9] to drag the enemy prefab onto the Enemy To Spawn box and the player target to the target box.

Test the scene.

Try adding multiple spawn towers.

You have now created an infinite spawn tower.

Terms definitions
2. function. A named section of a computer program that performs a specific task. Functions help make code more efficient and reusable. They may take input parameters and produce output.
3. parameter. An extra piece of information passed to a function to customize it for a specific need
4. component. The three strands of Technology in the NZC (Technological Practice[10], Technological Knowledge, and Nature of Technology) – together with the six specialist knowledge and skills strands for levels 6, 7, and 8) – support[12] the development of students’ technological literacy[11]. Each of these strands is broken down into smaller groupings (or substrands) called “components”. For […]
5. guide. A user guide, document or tutorial to explain[13] how to complete a task.
Terms definitions
1. variable. This lesson assumes that you have completed the guide on how to get an enemy object to chase the player using the nav mesh agent. You can find the lesson here. There is an example of an infinite enemy spawn tower which can be downloaded here. This will set an object that will infinitely spawn […]
2. function. This lesson assumes that you have completed the guide on how to get an enemy object to chase the player using the nav mesh agent. You can find the lesson here. There is an example of an infinite enemy spawn tower which can be downloaded here. This will set an object that will infinitely spawn […]
3. parameter. This lesson assumes that you have completed the guide on how to get an enemy object to chase the player using the nav mesh agent. You can find the lesson here. There is an example of an infinite enemy spawn tower which can be downloaded here. This will set an object that will infinitely spawn […]
4. component. This lesson assumes that you have completed the guide on how to get an enemy object to chase the player using the nav mesh agent. You can find the lesson here. There is an example of an infinite enemy spawn tower which can be downloaded here. This will set an object that will infinitely spawn […]
5. guide. This lesson assumes that you have completed the guide on how to get an enemy object to chase the player using the nav mesh agent. You can find the lesson here. There is an example of an infinite enemy spawn tower which can be downloaded here. This will set an object that will infinitely spawn […]