Battle City in Unity Part 6: Enemy AI
This article is Part 6 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 - This Article
- 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 - 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
Skynet. Anyone?
Sorry, we are not going to create a Skynet nor a Deep Blue nor something remotely capable of coming up with tactics to flank your tank trapping you in a corner where you cannot escape. These are beyond my meager knowledge; I apologize for my incapability. That could be something of a wishlist later if I manage to gain sufficient insight on that.
Instead, we will be using the most basic form of simulating AI, by randomness.
Heir to the Movement Script again
I mentioned in the previous post AI script is the other script which will inherit from Movement script. So let’s start by creating a script called EnemyAI and update the Class declaration to inherit from Movement.
Creating the unpredictable feeling
So what can we make random about the Enemy? The most straightforward one I can think of is its direction. By randomizing its heading, we will create a sense of AI. The code is pretty simple; we need to ask the computer to pick a random direction(up, down, left, right), then move in that direction if it’s selected. We will use
With that done, all we need to do is to add the same code in Player script which checks for h and v in FixedUpdate. Remember to do a GetComponent for the rigidbody 2d at the Start so that we can pass it to the coroutine as parameter.
Determine when to be unpredictable: Collision
Now we have the means to be unpredictable; the computer needs to know when it should be erratic. Looking through the gameplay of Battle City, you would have noticed that enemy tanks always changes direction when it hit an obstacle, so we could initiate a “RandomDirection” whenever we hit a barrier.
So let’s add the Monobehaviour routine
Also, add the routine RandomDirection at the Monobehaviour routine
I consolidate all the code for EnemyAI until now at the below. The code below is sufficient to make a working Enemy AI. You can try out by disabling the Player (Script) component from your guinea pig Tank and add the EnemyAI script. Press play and you can see your Enemy tank become self-aware and move by itself!
Determine when to be unpredictable: Random timing
We could also put another random timer for the computer to pick a moment to do an unpredictable turn; this timer should have a reasonable
The code for this is very simple, two lines only. One at the last line of RandomDirection routine to
Now try play your game again. Skynet has risen!
Determine when to be unpredictable: Logical unpredictability
Lastly, we will need to have the enemy to make a reasonable unpredictable(sounds contradicting) direction change. For example, an enemy tank makes its way to a location where it is surrounded on 3 sides with barriers, so the only logical option for it is to turn around and head out. But if we were simply to trigger the “RandomDirection,” the tank will just be spinning in multiple directions until it gets lucky enough to turn to face its back and head out. That is not AI(Artificial Intelligence), that is AS(Artificial Stupidity)!
To prevent AS from occurring, we will need to check for the direction the tank can turn to in order not to look stupid whenever the RandomDirection routine is triggered. So the best place to put this code will be in…..the RandomDirection routine! We will be using a
We will consolidate those position with a false LineCast into a list. Then the computer will run a
We will need two new variables for this effort, one for the LayerMask which I will call it
We will
For the LayerMask, we will declare it as a SerializeField variable so that the layers impacted can be chosen from the Inspector.
Once we have the list of directions we can turn to; we will pass it to the selection variable after making a random selection.
So the code will change from
Now we need to update the
In the end, your selection for blockingLayer should be like the below.
Duplicate a number of tanks in the scene. Play the game now and you will realize you have Skynet Mark II!
Below is the full code for EnemyAI. Next post I will show how we can do firing of shots.
This article is Part 6 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 - This Article
- 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 - 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