Creating a 2D Shooter game in Unity: Introduction
In this tutorial, we will learn how to create a simple 2D spaceship that fires missiles with Unity; some of the skills that you will learn along the way include:
- Creating sprites
- Instantiating Objects.
- Add and use Rigidbody physics components for 2D objects.
- Apply a force on an object.
Adding the spaceship
So, in this section, we will start to create the spaceship that will be used by the player; it will consist of a simple sprite (for the time-being) that we will be able to move in four directions using the arrow keys on the keyboard: left, right, up and down.
So, let’s get started:
- Please launch Unity and create a new Project (File | New Project).
- In the new window, you can specify the name of your project, its location, as well as the 2D mode (as this game will be 2D).
- Once this is done, you can click on the button called Create project (at the bottom of the window) and Unity should open.
- Once this is done, you can check that the 2D mode is activated, based on the 2D logo located in the top right-corner of the Scene view.
We will now create a new sprite for our spaceship; it will be made of a simple triangle.
- From the Project view, please select Create | Sprites | Triangle, as illustrated on the next figure.
- This will create a new asset called Triangle in the Project window.
- Once this is done, you can drag and drop this sprite (i.e., the white object with the label Triangle ) from the Project window to the Scene view; this will create a new object called Triangle in the Hierarchy view.
In the previous window, you may notice the white lines at the bottom and to the left of the screen; these are the boundaries that define what will be visible onscreen; so by dropping your object within these lines, you ensure that the player will be seen (or captured by) by the camera.
- Please rename this object player for now, using the Hierarchy window: to rename this object, you can right-click on it in the Hierarchy window, and then select the option Rename from the contextual menu.
So at this stage, we have a new player character (i.e., the spaceship) and we will need to move it according to the keys pressed on the keyboard; so let’s do just this:
- Please create a new C# script (i.e., select Create | C# Script from the Project window) and rename this script MovePlayer.
- Once this is done, please open this script and add the following code to it (new code in bold):
void Update ()
{
if (Input.GetKey (KeyCode.LeftArrow))
{
gameObject.transform.Translate (Vector3.left * 0.1f);
}
if (Input.GetKey (KeyCode.RightArrow))
{
gameObject.transform.Translate (Vector3.right * 0.1f);
}
if (Input.GetKey (KeyCode.UpArrow))
{
gameObject.transform.Translate (Vector3.up * 0.1f);
}
if (Input.GetKey (KeyCode.DownArrow))
{
gameObject.transform.Translate (Vector3.down * 0.1f);
}
}
In the previous code:
- We use the function Update to check for keyboard inputs.
- If the left arrow is pressed, we move the object linked to this script (i.e., the spaceship) to the left (i.e., 0.1 meter to the left).
- If the right arrow is pressed, we move the object linked to this script (i.e., the spaceship) to the right (i.e., 0.1 meter to the right).
** Note that we use the function GetKey that checks whether a key has been pressed; however, if you wanted to check whether a key has been released then you could use the function GetKeyDown instead.**
You can now save the script, check for any error in the Console window, and link the script (i.e., drag and drop it) to the object called player that is in the Hierarchy view. Once this is done, you can play the scene and check that you can move the player left and right. After pressing the arrow keys on your keyboard, you should see that the spaceship moves in four directions.
Note that to play and stop the scene, you can press the shortcut CTRL + P, or use the black triangle located at the top of the window.
Shooting missiles
In this section, we will get the player to shoot missiles whenever s/he presses the space bar; this will involve the following steps:
- Creating an object for the missile.
- Saving this object as a prefab (i.e., a template).
- Detecting when the space bar has been pressed (and then released).
- Instantiating the missile prefab and add velocity to it so that it moves up when fired.
First, let’s create a new object for the missile:
- You can now stop the scene.
- Using the Project window, please create a new circular sprite (Create | Sprites | Circle), and rename it bullet.
- Once this is done, please drag and drop this bullet asset to the Scene (or Hierarchy) window, this will create a new object called bullet.
- Using the Inspector, rescale this object to (0.1, 0.1, 0.1). The position of this object does not matter for now.
- Add a Rigidbody2D component to this object (i.e., select Components | Physics2D | Rigidbody2D from the top menu) and set the Gravity Scale attribute of this component (i.e., Rigidbody2D) to 0, as illustrated in the next figure.
By adding a Rigidbody2D component to this object, we ensure that we can apply forces to it, or modify its velocity; this being said, because we have a top-down view, we do not want this object to be influenced by gravity (otherwise it would fall down), and this is why we set the Gravity Scale attribute to 0 for this object.
- We can now convert this bullet to a prefab by dragging and dropping this object (i.e., bullet) to the Scene view.
- You can then delete the object called bullet from the Hierarchy.
Last but not least, we need to add some code that will be used to instantiate and propel this bullet if the player presses the space bar.
- Please open the script called MovePlayer.
- Add the following code at the beginning of the script (new code in bold).
public class MovePlayer : MonoBehaviour
{
public GameObject bullet;
– Please add the following code to the Update function:
if (Input.GetKeyDown (KeyCode.Space))
{
GameObject b = (GameObject)(Instantiate (bullet, transform.position + transform.up*1.5f, Quaternion.identity));
b.GetComponent<Rigidbody2D> ().AddForce (transform.up * 1000);
}
In the previous code:
- We create a new GameObject.
- This GameObject will be based on the template called bullet.
- If the player hits the space bar, the new bullet is instantiated just above the spaceship.
- We then add an upward force to the bullet so that it starts to move.
You can now save your script, and check that it is error-free in the Console window.
- If you click on the object called player that is present in the Hierarchy, and if you look at the Inspector, you should see that a new field called bullet has appeared for the component MovePlayer.
- Please drag and drop the prefab called bullet to this field.
Once this done, you can play the scene, and check that after pressing the space bar, you are able to fire a bullet.
This tutorial is an excerpt from the book “A Beginner’s Guide to 2D Shooter Games“.