Space Survival – Space Invaders Inspired Game

The second game that I made from the Game Design and Prototyping module was Space Survival, a clone of Space Invaders.

EXPLORATION OF IDEAS

I felt that this project was a good challenge after creating the Cookie Clicker clone. I now had some experience with Unity’s interface and the same with C# scripting through working on Business Builder. With the additional knowledge of Shape and Colour theory, I created my own sprites that fit with the space theme that I wanted to create.

As mentioned in my Shape and Colour theory post (https://usher-2021.hulldesign.co.uk/2021/10/16/shape-and-colour-theory/), I wanted to keep elements of the classic Space Invaders design. Using the rescale tool in Unity helped with this, as the assets within Space Invaders are relatively small whereas mine was not. I had made the sprites that I used within this project on a Photoshop canvas that had the dimensions of 5000 for the width, 5000 for the height and a resolution of 300 DPI due to that being my standard setup for everything I create and render. When I initially imported the sprites into Unity, the sprite would only be half-visible on the overhead camera due to their original sizing on the Photoshop canvas; this was quickly resolved by scaling the imported sprites down to a much smaller size.

I also made use of an old piece of art that I had created during College as the background to Space Survival. I used a red to maroon gradient masked over my old artwork to make it match the game thematically, as the image was originally using a colour palette made up of very light colours, such as pastel blue and yellow.

I knew that I wanted to take the next step in improving my C# scripting and Unity experience from what I had included in Business Builder and wanted to create a game that built off some elements that I had not included in the previous, such as creating a background and a score counter that increased as the player killed enemy units. I wanted to gain further experience with working with Unity’s engine and scripting in C#, and this project was a good way to try out that improvement.

Space Survival

SCRIPTING

With this project, I knew that I would be creating scripts that were a significant improvement from the scripts that were made in my cookie clicker clone, Business Builder.

The first script that I created allowed the player to move the spaceship along the x-axis when holding the left or right arrow keys. The speed of the player’s spaceship is controlled through editing the public float variable speed, which was set to 1. This was changed during testing in the Unity interface and significantly lowered to 0.3, as the player was moving extremely fast.

The player could also fire bullets at the enemies when they pressed the spacebar on the keyboard, which would spawn a prefab of the bullet object that I had created using Instantiate. A prefab is a type of component that saves fully configured game objects in the project for reuse (Unity Learn, 2020) whilst Instantiate in its most simplified form creates a game object within a space (Unity Technologies, 2021). The instantiate command was useful in this project, as it could create a copy of the bullet using the prefab upon the player pressing the spacebar, allowing the player to fire freely at the enemies without the worry of having no ammunition or the bullet missing the enemy.

The script controls the player’s movement when holding the left or the right arrow key and allows them to fire a bullet when they press the spacebar.

Another script that I created was the behaviour of the bullet that is fired by the player-controlled spaceship. This script had some elements that I included to help with the overall management of the game itself, such as destroying the bullet if it progressed past Y=6. This helps the game from having to process additional data for bullets that missed aliens, as they would continue to move up the player’s screen and continue off-camera – which would eventually begin to cause issues from potentially hundreds of bullets continually remaining in the game.

The bullet management section of the Bullet Behaviour script, destroying any bullets that progressed past y = 6 to improve optimisation.

Further on within my bullet behaviour script, I used an if statement to detect when the bullets were colliding with the enemy to trigger a number of things.

The statement checks if the object that the bullet is colliding with the tag “Enemy”, which for this project, is the aliens that progress down the screen. If the statement reads as true the alien and the bullet are destroyed and the player is rewarded with 10 points per kill on the score counter, which was created through following the YouTube tutorial “How to add a score counter into your Unity 2D game| Easy Unity 2D Tutorial” by Alexander Zotov (Zotov, 2017). Upon the alien dying, the alien would respawn through the use of an enemy prefab and the instantiate code that was used for the bullet, placing them in a random order along the specified area of the x-axis, just outside of the camera’s view to provide a constant steady flow of aliens for the player to kill.

If statement in the Bullet Behaviour Script

One thing that I did was create boundaries for both the player and for the enemy. I realised that if the player was unsuccessful in killing all of the aliens and one would pass by the player model, that alien would not respawn unless the player restarted the game within their browser.

To counter this, I created a cube that had a larger width than the overhead camera and placed it out of sight. I created an “Out of Bounds” script that I attached to the cube to detect enemies colliding with it, which would destroy any alien that was touching it and respawn it above the camera. By doing this, the game was able to keep a steady control on the aliens that were being spawned – effectively creating an endless shooter.

Player boundaries (shown in teal) and enemy boundary (shown in red)
Out of Bounds script attached to the enemy boundary

I also added player boundaries, as when I was testing the game I realised that the player could continue along the X-axis for as far as they want. The movement speed of the spaceship, combined with no restrictions along the X-axis made it a challenge to keep the player in sight of the camera whilst killing enemies.

To fix this issue, I used two cubes that I had extended past the height of the camera, placed them out of the players view and attached box colliders to them. With the addition of a polygon collider for the player’s spaceship, the player was now restricted to move only within the boundaries of the camera and to not fly along the X-axis for an indeterminate amount of time.

Due to still being relatively new to using Unity’s Engine and scripting in C# through Visual Studio, I found it easier to create these boundaries through the manipulation of the various colliders in Unity rather than through code.

CHALLENGES

The main challenge that I faced during this project was issues with Unity. I had made some basic errors, such as missing a semicolon of the end of a function or Unity autocompleting as I was typing – yet the main error that I faced I still do not entirely understand.

The player was able to shoot, move left and right whilst the enemy aliens progressed down the screen. Once one alien had been killed and had been asked to respawn, all of the aliens became impervious to damage – which confused me greatly. The aliens would not take any form of damage after one alien had died, essentially ending the game due to the enemy respawn condition not being triggered.

I remade the entire game on my personal computer after being met with several issues regarding opening the game itself – to find that the aforementioned error with the aliens being impervious to all damage fixed. The respawning mechanic worked, enemies could now die and the player could gain score.

OUTCOME

I learnt how to successfully create collisions with enemies through the use of Unity’s varying colliders and upgrade on the complexity of my scripting skills in comparison to the Cookie Clicker clone. From this, I was able to edit my scripts to create respawn conditions for the aliens once they had collided with the player’s bullets.

The score counter gives the game an aspect of competition, as without it the gameplay loop can be simplified down to Move Character -> Shoot repeating endlessly.

Space Survival being played

Itch.io link to Space Survival: https://rjusher.itch.io/space-survival

Referenced Material

Unity Learn (2020) Prefabs. Unity Learn. Available online: https://learn.unity.com/tutorial/prefabs-e#:~:text=Unity%202019%20LTS- [Accessed 17 Nov. 2021].

Unity Technologies (2021) Unity – Scripting API: Object.Instantiate. docs.unity3d.com. Available online: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html.

Zotov, A. (2017) How to add a score counter into your Unity 2D game| Easy Unity 2D Tutorial. YouTube. Available online: https://www.youtube.com/watch?v=QbqnDbexrCw.