In this article, we’re going to start adding some aesthetics to our game. Today we will be adding a background and adding sprites to our player, enemy, and laser. Firstly, I will be using assets that I downloaded from Filebase. Filebase is an asset collection for those who are members of GameDevHQ. For more information on GameDevHQ visit their site at, gamedevhq.com.
Now to the fun. If you have the images that you want to use ready, you can create a new folder and drag them from your file explorer to your folder in Unity. For this article, we’re using sprites. It’s possible that in the Inspector you will have to change the Texture Type to ‘Sprite (2D and UI)’.
We’re going to start with the background image. Just drag and drop the image into the Hierarchy. Rename the image as ‘Background’ in the Inspector. Now since we’re turning our 3D game into 2D we have a couple of things we need to do. In the top left corner of the scene view, there should be a button that has ‘2D’, click that so that our scene view is in a 2D perspective. Next, in the inspector with our background selected, look for the ‘Sorting Layer’, click the drop-down and choose ‘Add Sorting Layer’. Click the ‘+’ and create two new layers, first ‘Background’ and second ‘Foreground’ in that order. Now go back to your background and change the sorting layer to ‘Background’.
Since our game is 2D and we’re using sprites if we don’t start adjusting our sorting order we will have objects on top of other objects and that isn’t what we’re aiming for.
This is an example of what would happen. Sorting out our layers early will help us tremendously later in development.
On to the Player. It will be easier and quicker to remake the player, so find the image that you are using for your player sprite and drag it to the Hierarchy. Now change the tag to ‘Player’, the sorting layer to ‘Foreground’, and we’re going to add a couple of components. Add the ‘Player’ script and a Box Collider 2D component. Make sure that ‘Is Trigger’ is checked. The player is now done and you can delete the other from your Hierarchy. You might have to scale down your player since we created another. Sometimes the sprites will appear pretty big. You can also tighten the space of your box colliders if you need to by clicking on the ‘Edit Collider’ button under the Box Collider 2D component. You can just drag the lines over when that button is active and when you’re finished you click the button again. Below are my settings for the Player.
Now we’re going to move to the Laser. It’s easier to just rebuild this as well. Pretty much the same as the player. Drag your laser to the Hierarchy, make its sorting layer ‘Foreground’, add the ‘Laser’ script, add a Box Collider 2D component, and a Rigidbody 2D component. Make sure ‘Is Trigger’ is checked, ‘Gravity Scale’ is ‘0’, and tag it as ‘Laser’. Then position it where its starting location should be when it’s fired. Once you did that, delete the prefab laser we made before, and then make your new laser a prefab and remove it from the Hierarchy. The laser is now complete. Edit the collider if you need.
The enemy will be done the exact same way. Reconstructing these objects is much quicker in this stage than later down the road. Drag the enemy sprite to the Hierarchy, tag it as ‘Enemy’, add the ‘Enemy’ script, a Rigidbody 2D component, and a Box Collider 2D component. Set ‘Is Trigger’ to checked and ‘Gravity Scale’ to ‘0’. Now delete the old prefab and create a new prefab with this new enemy. Don’t forget to check the collider and edit it if you need to.
The only thing left with the ‘Enemy’ is changing something in the script. When we first wrote the script for the enemy we were working in 3D. Now we pivoted to 2D and have to adjust our triggers to accommodate the new 2D aspect of our game. Open the ‘Enemy’ script and head to our OnTriggerEnter function. Literally, all we have to do to switch this over is to add 2D after both ‘OnTriggerEnter’ and ‘Collider’.
Just test the game and make sure it plays as it did before. Make sure the player moves and shoots, ensure the lasers hit the enemies and they die. Also, make sure the enemies die when they hit the player and that the player can die after 3 hits. Once all that is checked, then we’re done with this part of the process.