Using what I’ve learnt from the Space Invaders project in terms of the fundamentals in the shooting, movement and respawn mechanics, The Top Down Shooter project would use similar functions but with more character movement and objects colliding with the player ( used as cover for the player ) as well as more movement for the enemies.
For the Top Down Shooter, I wanted the game to be a continuation / sequel to the Space Invaders Prototype so I added the same character and overall aesthetics, just different enemies.
The idea was for the game to be set where the character was floating in space with debris surrounding the level as well as enemies respawning in different locations.
To start off with, I added a Rigidbody 2D component and a Polygon Collider to the player sprite so it can react to different objects on the map and so it allows the sprite to stimulate physics.
Scripting: Player Movement
Unlike the Space Invaders Prototype ( which only focused on horizontal movement ) , the player for the Top Down Shooter can move within the horizontal and vertical axis. This allows the player to roam around the level and interact with the cover more frequently, adding strategy to the game.
I also added a speed variable to adjust the player’s speed, much like in my Space Invaders Prototype.
A new feature I’ve added to the game, however, was the ability for the player’s sprite to follow the mouse pointer position, making the controls for the bullets much easier to use during combat.
So to begin with, I added a Camera and Mouse position variable so the position is referenced from the scene view and the camera can be referenced for this:
This function converts the mouse’s position into ingame x,y coordinates so now it can be referenced within the sprite’s rotation.
Then I created a FixedUpdate function so the angle and position of the mouse would update every frame in tune with the RigidBody’s position.
Scripting: Bullet Behaviour
For my Bullet Behaviour script, I mainly used what I learnt from the Space Invaders Project and transferred similar code into this project. Same as before, I created a Projectile Prefab so Unity can reuse this object multiple times.
These Variables will help identify where the bullet’s will spawn and what prefab will be used.
the public float: Bullet Force adjusts the speed of the bullets when they’re fired. This can be used to potentially add a level of difficulty to gameplay since if the bullets are slower in speed, the player would have to time their shots when dealing with enemies.
The input for the shooting mechanic shown here is the left mouse button since “Fire1” is mapped to that control in Unity.
So when this “if” statement is activated, it goes over to the “shoot” function where it spawns in the projectiles as well as adding the bullet force to them as well as adjusting the angle depending on the angle of the bulletspawn location. The Bullet Spawn Location object is an empty one but it’s attached to the player sprite.
For Collision script, I wanted to make sure that when the bullets collide on cover objects or the invisible barriers, they would despawn, this was a work-around to make sure that stray bullets wouldn’t affect gameplay (for example, despawning enemies randomly and making open spots harder to see.).
This essentially meant that if objects tagged as cover were to interact with the bullets, they would despawn automatically, this included invisible walls too which were close to the edges of the game view.
For all the cover objects, I added box / polygon colliders so enemies would have to work around to catch the player but the player can use them to hide around them.
For the enemies, I used similar code except I wanted them to respawn so using my knowledge from the space invaders project.
In summary: when the bullet collides with the enemy, both the enemy and the bullet will despawn. However, with the instantiate function, the enemy will repsawn with a different position depending on the range in the Vector3 function.
Scripting: Enemy Behaviour
To start off with creating enemies, I wanted to make the sprite into a prefab since there will be multiple enemies on one level which will respawn multiple times.
I wanted the enemies to follow the player which means replicating their movements to suit the player. I added a rigidbody to the enemy prefab and started working on it’s movements.
Since I wanted the enemy to follow the player, I needed to reference them in the a private float. Once the player’s rigidbody (the Main Character Sprite) is referenced, then the I added GameObject.Find which referenced the character sprite and meant that the enemies focus was towards them.
In the FixedUpdate Function, I used similar script to the player movement, in order for the enemy’s movement to replicate theirs, adding a level of challenge to the gameplay experience.
The enemy’s function is to get closer to the player so everytime the player moves around, the enemy will follow those coordinates so they move towards them in real time.
The challenge with this project was replicating some of the features from the Space Invaders project such as the randomisation for the respawn coordinates, since I could only have the enemies repsawn within the scene because of the invisible walls I’ve implemented. However, I reminded myself on how the axis works using the transform tool in Unity.
This project helped refine and improve the mechanics learnt from the space invaders project with free roaming movement and adding functions that’ll allow better combat when fighting enemies but also making enemies more of a threat towards the player. Although, I’m not a huge fan of the Top down Shooter genre, this project has certainty cemented the idea of implementing a more strategic build for future games.
Software Used: Paint Tool SAI, Unity 2D, Microsoft Visual Studio