Battle City in Unity Part 8: Hurting the tanks
This article is Part 8 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 - This Article
- 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 - 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
The real deal starts here
Now is time to do some damage. Destroying bricks or Steel Wall are no fun since they are lifeless, but defeating the enemy brings satisfaction. Surely you can hear your computer groaning when you beat it in a game or the evil grinning sound it makes when you go Game Over. This is the real deal. Let’s get started!
Dealing Damage
What do you mean by dealing damage? The tanks in this game are weak; it gets destroyed once hit by a CanonBall. Well, you are only partially right. There are instances where tanks do not get killed with a single shot in Battle City. For example, when a tank gets spawned, there will be an electric aura around it where it is “invincible”, so technically you are dealing damage to it, just that it is not significant to kill it. Or another instance when the armored tank comes into the picture, you will need to strike it 4 times before it gets brought down so we can say that it can take 4 damage.
What I am going to do is to fulfill all the single damage, 4 hit damage and invincibility all in one by letting it take damage.
How to take damage
Start by creating a new script under SmallTank GameObject calling it
actualHealth is the health that the tank should have at the beginning. So all types of tanks should have health value of 1 other than the Armored Tank which has 4. We will default that to 1 and will change it in the prefab later if required.
currentHealth is how much health the tank has currently so an armored tank that is shot at twice will have its currentHealth as 2.
Create a new public routine called
Create two public routines called
Now let’s create the code for the
The best way is to create a
We will leave the tagging till we finished creating our tank and duplicate it to make the rest of the tank types. For now, we will just take note of the Player and Enemy tag.
We will do a
If it is the enemy, do a comparetag for each of the tag and add one to the static int in MasterTracker for the type of tank destroyed.
Once done, destroy the Game Object. So the consolidated code for the Heath script will be as below. The Spawn Player and Gameover will be handled in other posts when we talk about spawning the tank and GameManager. Also update the TakeDamage routine replacing the trigger death routine comment with Death routine.
Triggering TakeDamage
Now the script for
Summary and Full Code
Now let’s try out our CanonBall’s ability to kill a tank. I have adjusted the CanonBall’s speed higher so it does not look anti-climatic. Disable Player script or EnemyAI if you have that in your tank so it cannot escape from its inevitable death.
With this we complete the Projectile script and its ability to damage the tanks, bricks and Steel Walls. Next up is giving the tanks ability to fire. The full code for Projectile is as below.
This article is Part 8 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 - This Article
- 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 - 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