GLI: Framework 1
Continuing from the previous article we will now implement the ability to instantiate the AI agent from one waypoint which the AI agent will then automatically try to reach the last waypoint. Also while instantiating the AI agents we will also insert them into an object pool to keep track of them. Another bonus for adding an object pool is to control the amount of AI we can spawn at any given time.
First we are going to create several new scripts, One that is going to control the spawning, and one that is going to managed the spawn (aka spawn Manager).
The Spawn Manager:
The Spawn Manager is going to focus on creating the spawn function and also manage the Object Pool.
Since this will be a manager class script, we will be required to make it a singleton. If you don’t know what a singleton is or require a refresher, the article here will be quite useful.
After stating the singleton, we will need to create several local variables and also create the List which will house all the spawned objects. We will need to identify the Enemy AI spawn game object, the int variable for the seconds required until the next spawn and finally the Transform variable for the starting location (which will be the waypoint A).
In the start method we will need to create a new List for our Pooled Objects. We will also need to setup an for loop with a cap limit of “spawn Amount” as our threshold, otherwise we will continue to spawn in the AI game object. Once the AI has been spawned we are going to deactivate them. There is a reason behind all this.
Think of this setup as a loaded gun with ammo, right now we are loading in a cartridge with a preset amount of ammo. We don’t want to fire/use them right away, only when we pull the trigger shall the ammo be presented.
To create the actual firing mechanism, we will need to create a public method which will turn on the hidden objects in the object pool.
Creating the Spawn Timer:
Now it is time to create the spawn timer (or the trigger) to spawn the AI agent. I created an new C# script called Enemy Spawning. Inside the script I started with an serialized int that will control the timing per spawn.
Since I want the timer to be random, I created an random range within the update method which sets the update int value from 5f to 10f. I also added in a custom method called Enemy Spawning, and this method is what controls the Coroutine that allows the spawn timer to function.
Inside the Coroutine “Spawn Timer CD” we are going to reference the spawn manager via the singleton and access the public method “Get Pool Object”. Then we are going to add in the yield return + the random spawn timer along with setting the Enemy AI game object to active.
Since the Enemy AI game object has all been preloaded inside the object pool, the compute workload is much more optimized during game play than if you were to do a regular instantiate during runtime.
Returning back to Unity, make sure that the Enemy Spawn Manager Script has the required components in the slots before playing.
Once the game is ready to test, you will see that the object pooling already starts with the dedicated set amount. (I wrote in 5 spawns and the object pool already has 5 preloaded into the pool) And once the spawn timer is up there will be a new spawn.