Battle City in Unity Part 4: Tank Movement
This article is Part 4 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 - This Article
- 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 - 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
Prep Works
Now I want you to delete the rest of the tanks you have created leaving just one. You know that it is straightforward to create the rest of the tanks so we can do that again later. We only need one tank right now as we will be making a lot of modification on this Game Object and the same modification will be made on all the types of tank. So might as well we do all the modification on one then duplicate them at the end.
Next up, we will taking care of the movement of the tanks. They are main moving objects in this game. The enemy tanks and player tank share similar movement except that one is controlled by the computer while the other is controlled by the player itself. What I am going to do is to create a class to hold all the movement essentials which shared by the Enemy Tanks and Player Tanks. With this, we will make this class an
To create such a class, we will create a C# script from the Project Window and name it Movement. Open the Movement script in Monodevelop or Visual Studio whichever you preferred. Insert the word abstract to the class declaration line in the Movement script so it will read as the below.
Go to Youtube to observe the movement of the tanks done in the Battle City Game. You will realize:
- The tanks will turn to face the direction it is moving before moving forward.
- The tanks will only move in up, down, left, right directions(no diagonals).
- The movement of the tanks are perfect in a sense it will definitely turn at the exact per unit(e.g., 1,1 or 1,2) point. The perfect movement, in turn, will prevent scenarios like getting trapped as moving to a sub-unit point (e.g., 1,1.23 or 2.3,1.57) will mean unnecessary collisions with the tiles which are lined up precisely in per unit point. These collisions in turn affect the gameplay experience.
- Movement is smooth and not jerky.
Tank Movement Plans
There needs to be some input for the script to determine which direction to move. Since no input will be expected by the enemy as that is part of AI, we will look at how we can get user input. We will get input from the user is via the input manager using the command
We will not be allowing diagonal movement, so we will split the routine into Horizontal Movement and Vertical Movement to have better control over which movement has priority. I created two routines called
The coroutines will be with
Tank Turning
The first part we need to handle is the rotation(turning of direction). The code is pretty much straightforward, we just need to set the rotation using
Turning to face left: Quaterion.Euler(0,0,90) or Quaterion.Euler(0,0,-270)
Turning to face top: Quaterion.Euler(0,0,0) or Quaterion.Euler(0,0,360)
Turning to face down: Quaterion.Euler(0,0,180) or Quaterion.Euler(0,0,-180)
Tank Moving Failsafe
There are 2
The second one is a boolean(i call it
The isMoving flag will also be of protected permission so that the class inheriting it can determine if the execution of the coroutine is completed below triggering the next movement.
Tank Movement
Although each execution of the movement coroutine is guaranteed(at least in theory) to move 1 unit space, we cannot be moving it abruptly to its destination. Instead, it should be gradual to ensure there is continuity when moving to the next execution of the movement coroutine if not we will end up with a visual faux pax.
To do this, we will be doing a
Summary and Full code
This marks the end of tank movement creation. Next post we will talk about how to allow the player to control the tank.
This article is Part 4 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 - This Article
- 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 - 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