Ok, so we have been playing around with cubes and spheres. The cube being the player, the sphere our enemy. It’s time to add some cool looking spaceships, real lasers oh, and a galaxy to fly in!
So first thing first — let’s get the cool looking 2D sprites into our Unity Editor. Because my assets are all located on my computer I can easily attach them to my game by just dragging them into the Project folder. Make sure you have the folders all ready to go and easy to understand. Making folders for your sprites and audio is imperative to keep things organized.
As you can see inside my Sprites folder I have it broken into sections such as Player Shield, Explosion, etc. Also note that I have an Audio folder that I placed all of my Audio file into.
Just a quick note here that you may be getting assets from the Unity Store. In this case you will need to import them into Unity — don’t worry it’s an easy process and one that we will touch on in another article.
So now we have our assets and are ready to go. We now need to change our 3D prototype into a 2D game. Remember we were prototyping this to begin so we could get our code started and the main frame of our game built. We used the 3D just so we could easily create a prototype player and enemies. It’s easy to change into 2D mode.. have a look.
And just like that, voila, 2D. Pretty easy eh?
Now let’s set up our galaxy and see how this is going to look. I’m just going to take my Galaxy Sprite and drag it into the Hierarchy Window like so…
Note in the Inspector window that when I drag it into the Hierarchy it shows Sprite Renderer. There is no longer need for a Mesh Renderer because we are working in 2D.. sprites.
Ok, so let’s add the Player. Do the same thing as the Galaxy, just click on the Player sprite and drag it into the Hierarchy. Now, a very important part of working in 2D is layers — since 2D isn’t 3D and has no depth you have to give the sprite layer order to see which is rendered on top of the other.
Check out the gif below, when I drag the player into the Hierarchy and then move him around note that he is behind the planets and stars. This is layers. We want to make sure he is set in front of them.. note how I change the Order in Layer to 1. The higher the number in this layer system the closer the object will be to you. So if you want something to be above the ship you would layer that 2. See below:
Now, we need to delete the prototypes. They have served their purpose. Sail on my beautiful cube and sphere…
BUT WAIT! Don’t just go deleting all willy nilly. Remember we attached some components to those prototypes. You know, like the scripts that make them actually do things?
When I replace the old with new I always keep the original so I can see what components are needed. Either that or a pen and paper will suffice.
With my Player Cube I had a Player Script, Box Collider that I want to attach to our new Sprite. Now here’s the important thing to remember — when you are moving to a new game object a lot of the old settings are going to be gone. Things like Tags, Script settings etc. I find it always a good idea to test BEFORE deleting.
For example, when I add the Player Script to the new 2D object it is missing the Laser Prefab. A simple way to ensure all of the values are set properly is right click on the 3D player Script component, Copy Component and then Paste Values — see below:
Now, don’t forget to tag the Player again — top of the Inspector you will see Tag Untagged. Just drop the box down and click Player.
The last thing you are going to need is a Box Collider. We need something that will sense any collisions. Now that we are in 2D mode we only need a 2D Box Collider. Note that you will need to resize this so the collisions are going to be accurate. To do this click on the Edit Collider and then shrink the box to make collisions only happen when the enemy hits the ship.
Now, you will need to do the same for the Enemy. Make sure you have ALL components and values set up for the Enemy. Test your game a lot, it’s the power of Unity. If you run the game right now you will see there is no collisions, so you change that. Test again, fix the problem and test again, rinse and repeat.