Battle City in Unity Part 22 : Bonus Crates - Grenade
This article is Part 22 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 - 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 - This Article
- 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
Next up on the Bonus Crates. We will touch on the Grenade bonus crate which is symbolized by the grenade symbol. Its effect is to destroy all enemy tanks in the gameplay area instantly, but the enemy tank reserve will not be affected.
Start by dragging and dropping the Sprite you have for grenade bonus crate into the hierarchy which Unity will help to create the Game Object for you. Call the Game Object
Creating the grenade effect
The key to creating the grenade effect is to be able to find all the enemy tanks on the scene. But if you followed faithfully through all these tutorials, you would have realized all the enemy tanks clones are all around the hierarchy, and it looks very messy. So we will need a means to keep them neatly using an empty Game Object as a parent to “look after them”. Create an empty Game Object in the hierarchy calling it
Code modification
Now to move all the spawned enemies we need to get a reference to
Then all that’s left is to make a newly instantiated enemy tank as a child to the enemyHolder in the StartSpawning routine in Spawner script. The updated code for StartSpawning routine will be as below.
Let’s test to see if the Enemy Tanks are now “behaving properly” under their parent EnemyHolder.
Now they are obedient children, and the hierarchy looks tidier too. Now we can get hold of all the enemy tanks in the scene easily by asking its “parent”. Let’s start to code on the Grenade. Add a new script called
The boolean parameter value of true is to get the references of those inactive Game Objects as well(the tanks are disabled at the start of spawning animation). Once that is done, it will destroy the game object.
Let’s also update the
Then we can update the OnTriggerEnter2D of Grenade script to add in the parameter of TakeDamage to a large number so that it can destroy even those newly spawned enemy tanks(initial invincibility grants them 1000 health points) and also setting the flag of destroyedByPowerUp to true.
Now we can test. I will delibrately destroy just 1 tank while leaving the rest to be destroyed by the grenade and see if the Score Scene takes in the accurate information.
Working as expected. You can prefab the Grenade now. Let’s move to the harder Bonus Crate effects starting with Stopwatch.
This article is Part 22 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 - 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 - This Article
- 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