Battle City in Unity Part 13 : The Gamemaker - Spawning
This article is Part 13 in a 25-Part Series.
- Introduction - Battle City in Unity Prequel
- Part 1 - Battle City in Unity Part l: Single(ton) Sole Survivor
- Part 2 - Battle City in Unity Part 2: Level Creation using Tilemaps
- Part 3 - Battle City in Unity Part 3: Creation of the Protagonist and Antagonists(Tanks)
- Part 4 - Battle City in Unity Part 4: Tank Movement
- Part 5 - Battle City in Unity Part 5: Player Controller
- Part 6 - Battle City in Unity Part 6: Enemy AI
- Part 7 - Battle City in Unity Part 7: Creating Projectiles
- Part 8 - Battle City in Unity Part 8: Hurting the tanks
- Part 9 - Battle City in Unity Part 9: Firing the shots
- Part 10 - Battle City in Unity Part 10: The Spawn
- Part 11 - Battle City in Unity Part 11 : The Gamemaker - Starting the Stage and Game Over
- Part 12 - Battle City in Unity Part 12 : Level Manager
- Part 13 - This Article
- Part 14 - Battle City in Unity Part 14 : The Gamemaker - Stage Cleared or Game Over
- Part 15 - Battle City in Unity Part 15 : Spawning Invincibility
- Part 16 - Battle City in Unity Part 16 : Exploding Tank animation
- Part 17 - Battle City in Unity Part 17 : Score Calculation
- Part 18 - Battle City in Unity Part 18 : Battle Status Board
- Part 19 - Battle City in Unity Part 19 : Bonus Crates - Generation
- Part 20 - Battle City in Unity Part 20 : Bonus Crates - Tank Extra Live
- Part 21 - Battle City in Unity Part 21 : Bonus Crates - Tank Invincibility
- Part 22 - Battle City in Unity Part 22 : Bonus Crates - Grenade
- Part 23 - Battle City in Unity Part 23 : Bonus Crates - Stopwatch
- Part 24 - Battle City in Unity Part 24 : Bonus Crates - Level Up
- Part 25 - Battle City in Unity Part 25 : Bonus Crates - Shovel
One of the critical roles of the GamePlayManager is to trigger the Spawning of enemies and Player(The actual spawning is done by the spawn point). Being the manager means it gets to choose where and when to spawn. To be able to decide where to spawn, it will need to know where are the spawn points.
Let’s start with the prefab of SpawnPoint and PlayerSpawnPoint which we created at the end of Recreate Battle City in Unity Part 10: The Spawn. Create and assign a tag for the SpawnPoint prefab called
Now let’s get back to the
How to trigger the Spawn
Now we have the references to the Spawn Points. Let’s write the code for triggering the spawning.
We start off with the spawning of enemies. Create a routine
Next, we need to choose the SpawnPoint. We do that by randomize choosing one of the elements of the array of SpawnPoint GameObject and then trigger the Animator variable
Now let’s test it out!
- Set value of 1 to the smallTankInThisLevel in the LevelManager Component of Canvas
- Drag and drop some SpawnPoint prefabs onto the Hierarchy. Make sure it is within the game play area.
- Add the SpawnEnemy routine to the StartStage coroutine ensuring it is triggered after the curtain raiser sequence by adding a yield return null
- Update the Start Monobehaviour as below.
- Enable back all the Canvas UI elements if you have disabled it earlier. Now start playing! Play it a few times to ensure it spawns randomly at different Spawn Points.
Continuous Spawning
So now we can spawn, but we need to keep spawning until we run out of enemies. We will be using a routine called InvokeRepeating to the SpawnEnemy routine.
Let’s update the StartStage coroutine to make the changes.
I decided to add set the Spawn frequency as a Static variable from LevelManager so that we can change it from the LevelManager the speed of the spawn.
So update the LevelManager Script as below.
Then update the InvokeRepeating for SpawnEnemy to the below.
Stop Spawning
Once the enemy exhausted its reserve of tanks, we will need to stop the spawning. We will do this in the SpawnEnemy routine itself where we have an “if” statement to see if there are any more enemy tanks left. We will add a CancelInvoke command to the “else” portion of the statement.
Which enemy tank type to spawn
We can now spawn tanks continuously. But each stage has their own varying number of different tank types to spawn. If LevelManager states that there are only 10 small tanks, we must be able to stop spawning small tanks. Instead, we should be spawning the other types that still have in reserve.
The honor of doing this job will be handled by the Spawner itself as it is the one picking from random which tank to spawn.
We will be using something similar to the RandomDirection routine where we add the direction that has no blocking to a list before asking the computer to pick 1 from the list randomly .
This time we will add the type of tank that still has quantity in reserve based on the number of each of the tank type left to be spawned to a list(i called it
So let’s head to the Spawner script. First, we need to have an enum with each tank type for easy identification.
Then we can check with the
Then we can ask Skynet to randomly pick from the list the tank type it still can spawn.
Of course, we will later need to deduct the tank spawned from its reserve.
We also need to ensure the above code will only be for the enemy’s spawn point. So we need to check the
Let’s test out the spawning of enemies again. Add 3 SpawnPoint prefabs to the hierarchy, make sure they are in the gameplay area. Go to the Canvas GameObject under the Level Manager (Script) component in the Inspector and key in 3 each for small, fast, big, and armored tanks and set the Spawn Rate to 1.
What about the player?
Oops, we forget about the player. The code is relatively similar to that of SpawnEnemy. It is simpler as there is only 1 spawn point so we do not need to randomly pick a location for it. Similar to SpawnEnemy, we will do a check on MasterTracker’s playerLives to ensure it is not zero before we can allow the spawning. If it is zero, then it is GAME OVER! If we trigger the spawning of player, we will also need to deduct it from the playerLives other than when it is spawning the player tank at the start of the stage. So we need to declare a boolean called
Add to the StartStage Coroutine as well to spawn the player’s tank at the beginning of the stage.
Let’s head back to the Health Script’s
Now Let’s try to test the game again, with a PlayerSpawnPoint as well. I will try to beat the breakneck spawn speed enemies with my 3 lives and see who lives to tell about it. Bring it on, Skynet!
Ok I lost. Long Live SkyNet! At least my script works, for a consolation.
Fixing the Stage Number
OK I am fedup with the “New Text” word appearing at the start of stage. So let’s make it show the Stage Number. Add the below to the first line of the Start Monobehaviour.
Update the Canvas’s LevelManager component’s Stage Number to 1. And press play. Phew! One eyesore resolved.
That’s all for Spawning part of GamePlayManager. Next we will move on to how do we clear the stage.
This article is Part 13 in a 25-Part Series.
- Introduction - Battle City in Unity Prequel
- Part 1 - Battle City in Unity Part l: Single(ton) Sole Survivor
- Part 2 - Battle City in Unity Part 2: Level Creation using Tilemaps
- Part 3 - Battle City in Unity Part 3: Creation of the Protagonist and Antagonists(Tanks)
- Part 4 - Battle City in Unity Part 4: Tank Movement
- Part 5 - Battle City in Unity Part 5: Player Controller
- Part 6 - Battle City in Unity Part 6: Enemy AI
- Part 7 - Battle City in Unity Part 7: Creating Projectiles
- Part 8 - Battle City in Unity Part 8: Hurting the tanks
- Part 9 - Battle City in Unity Part 9: Firing the shots
- Part 10 - Battle City in Unity Part 10: The Spawn
- Part 11 - Battle City in Unity Part 11 : The Gamemaker - Starting the Stage and Game Over
- Part 12 - Battle City in Unity Part 12 : Level Manager
- Part 13 - This Article
- Part 14 - Battle City in Unity Part 14 : The Gamemaker - Stage Cleared or Game Over
- Part 15 - Battle City in Unity Part 15 : Spawning Invincibility
- Part 16 - Battle City in Unity Part 16 : Exploding Tank animation
- Part 17 - Battle City in Unity Part 17 : Score Calculation
- Part 18 - Battle City in Unity Part 18 : Battle Status Board
- Part 19 - Battle City in Unity Part 19 : Bonus Crates - Generation
- Part 20 - Battle City in Unity Part 20 : Bonus Crates - Tank Extra Live
- Part 21 - Battle City in Unity Part 21 : Bonus Crates - Tank Invincibility
- Part 22 - Battle City in Unity Part 22 : Bonus Crates - Grenade
- Part 23 - Battle City in Unity Part 23 : Bonus Crates - Stopwatch
- Part 24 - Battle City in Unity Part 24 : Bonus Crates - Level Up
- Part 25 - Battle City in Unity Part 25 : Bonus Crates - Shovel