Battle City in Unity Part 19 : Bonus Crates - Generation
This article is Part 19 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 - Battle City in Unity Part 13 : The Gamemaker - Spawning
- 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 - This Article
- 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
We are now on one of the unique features of this game which is designed to help the players: Bonus crates.
Information about the bonus crates
From what I gathered from Wikipedia, Battle City has 6 types of bonus crates.
- Grenade: This will destroy all the enemy tanks in the gameplay area. The enemy tank reserve will not be affected.
- Star: Upgrade the Player Tank Level. This can go up to Level 4. Level 2 increases the canonball speed. Level 3 allows two simultaneous canonballs to be fired. Level 4 bestowed destroy steel wall ability. The level-up carries across stages unless the tank is destroyed, which will reset the tank back to Level 1.
- Stopwatch: Freezes all enemy tanks in the gameplay area for a period of time.
- Shield: Make Player Tank invincible for a period.
- Tank: Give 1 extra life to Player.
- Shovel: Adds steel walls around the Eagle for a period of time. It also repairs any prior damage on the wall, unless the player destroys the steel wall.
- How the bonus crates gets spawned on Gameplay area is upon destroying an enemy tank which is flashing red.
- The spawning of the flashing red tanks are by random.
- The bonus crate type spawned is also random.
- Location of the crate is random as well.
Creating the flashing red tank
We need the flashing red tank to indicate destroying this tank will spawn a bonus crate. We will create a script on the SmallTank Prefab called
Now it’s time to add the random creation of the bonus tanks. We will do it in the Spawner script. All we need to do is to add the below which gives a 20% chance that a tank spawned will be a bonus tank. This will be added
Let’s try it out with 20 enemy tanks and see if we will get around the 20% chance of bonus tanks. I have disabled the PlayerSpawnPoint and the Eagle to prevent moving to the next stage.
OK. Not bad, we got a 15%(3 out of 20). Testing successful! Let’d put this frequency 0.2 elsewhere where we can change easily per level. So let’s move it to LevelManager script. The updated LevelManager script will be as below with new float variables
Then we can update the Spawner script’s statement for the random rate of bonus tanks generation from
Creating the bonus crates
Now we move on to the creation of bonus crates, and this will be handled by the
The key thing for the instantiation of the bonus crate is its position. We can not be instantiating the crate at locations where the player tank can’t reach(Water, steel walls and outside of the gameplay area), that would look silly. To do that we will need to get references to the tilemaps so that we can determine if the location where the crate is not appearing in those tiles. Declare
At the Start Monobehaviour, get reference to SteelTileMap and WaterTilemap using GameObject.Find.GetComponent.
We will create a routine called
So we can now create the routine for generating the bonus crate. It will be called
Telling GamePlayManager to create bonus crate
This is now very straightforward. All we need to do is to run the
That’s all the code we need to generate the bonus crate. Next post we will talk about creating the various types of bonus crates and coding its effects starting with the additional live bonus crate.
This article is Part 19 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 - Battle City in Unity Part 13 : The Gamemaker - Spawning
- 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 - This Article
- 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