noobtuts

Unity 2D Space Shooter Game

Unity 2D Space Shooter
In today's Tutorial we will learn how to make a 2D Space Shooter Game in Unity with less than 100 Lines of Code. As usual everything will be explained as easy as possible so everyone can understand it.

Foreword

Our 2D Space Shooter will be inspired by the old arcade games that everyone loved when growing up.

We will make use of all kinds of different Unity features like Physics (including Rigidbodies and Colliders), Animations (Mecanim), Scripting (C#), Prefabs, Shaders and the Sprite Editor. The game art will be very easy to create with our drawing tool of choice.

Requirements

Knowledge

We will explain every single step in detail, but it's still a good idea to get a basic overview of Unity if you never used it before. Feel free to read through our easier Unity Tutorials like Unity 2D Pong Game to see what Unity is all about.

If you used Unity before then we can get started right away.

Unity Version

Our Space Shooter will use Unity 5.0.1f1. Newer versions should work fine as well, older versions may or may not work. The free version of Unity 5 now comes with all the engine features, which makes it the recommended version.

Creating the Project

Let's get to it. We will start Unity and select New Project:
Unity New Project

We will name it spaceshooter, select any location like C:\, select 2D and click Create Project:
Unity Create new 2D Project

Making the World Black

At first we will select the Main Camera in the Hierarchy and then take a look over to the Inspector. We will change the Background color to black and adjust the size to 10:
Unity Camera Properties

Creating the Background

The Space Texture

Our background should consist of an image of stars that scrolls downwards so it appears as if the player is flying through space. We will begin by drawing a 256 x 512 px space background in our drawing tool of choice:
Pixel Art Space Background
Note: right click on the image, select Save As..., navigate to the project's Assets folder and save it in a new Sprites folder.

Once saved into our Project, we can select the background image in the Project Area:
Background in Project Area

Afterwards we can modify the Import Settings in the Inspector:
Background Import Settings
Note: usually we would set the Texture Type to Sprite for a 2D game. But scrolling a texture with UV Mapping only works for Textures, not for Sprites. Sprites would be pixel perfect, but scrolling them requires a Shader that would be far too complicated for this tutorial.

Adding it to a Quad

Alright, let's add the background to our game. We will select GameObject->3D Object->Quad from the Top Menu in order to add a Quad to our game:
Quad in Scene
Note: we use a Quad from the 3D Object menu in a 2D game because a Texture (instead of a Sprite) can be added to it.

Now we will take a look at the Inspector and scale the quad so it has the same aspect ratio as our background texture:
Quad Scaled

We will also rename it to Background:
Quad Renamed

And remove the Mesh Collider because we won't need it (it's for 3D games):
Remove Mesh Collider

Afterwards we can drag the background image from the Project Area onto the Background:
Quad with Default Material

If we press Play then we can now see our space background, which is still very dark:
Dark Background

The Unlit Shader

The background is dark because it currently uses the Standard Shader, which only makes things bright if there are lights in the Scene. We won't use any kind of lights or shadows, so let's select the Unlit->Texture Shader:
Background with Unlit Texture Shader

If we press Play again then we can now see the background stars without being too dark:
Unlit Background ingame

UV Mapping

If we take a closer look at the Shader properties then we can already see our UV Tiling and Offset here:
Background UV Properties

Feel free to play around with those properties and see how they change the background. Tiling will repeat it and Offset will change the texture's position. If we set the y Offset to values like 0.1, 0.2, 0.3 and so on then we can already see the scrolling happen.

Of course, we don't want to change the offset manually 60 times per second in order to achieve a scrolling effect. We will write a Script that takes care of it. Let's click on the Add Component Button on the Quad, then select New Script, name it UVScroll and select CSharp for the language:
Background UVScroll

Unity just created a new C# Script in our Project Area and added it to the Quad. Let's create a new Scripts folder in our Project Area and move the Script into it:
UVScroll Script in Project Area

Now we can double click the Script in order to open it:

using UnityEngine;
using System.Collections;

public class UVScroll : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
}

We won't need the Start or the Update function so let's remove them. It's considered best practice to modify the UV Mapping in the LateUpdate function after all other calculations were finished. We will also add a public variable that let's us modify the Scroll Speed in the Inspector later on:

using UnityEngine;
using System.Collections;

public class UVScroll : MonoBehaviour {
    public Vector2 speed;

    void LateUpdate() {
        GetComponent<Renderer>().material.mainTextureOffset = speed * Time.time;
    }
}

Note: while it looks really simple, there are quite a few things happening behind the Scenes here. First of all, we used a Vector2 for our speed variable to make sure that we can modify the x (horizontal) and y (vertical) speed. We use GetComponent<Renderer>() to access the renderer component. Using GetComponent<Renderer>().material makes sure that the UV offset is not modified permanently, as it would be the case when modifying it by hand. Instead by using GetComponent<Renderer>().material Unity creates a runtime copy of the Material and deletes it after the game is stopped. Furthermore we use the mainTextureOffset property to modify the main texture's UV offset. We then use Time.time to achieve the Scrolling effect because Time.time is the elapsed time since the beginning of the game. And since this time always increases smoothly, we might as well use it for our Scrolling. Furthermore we multiply the time by our speed to slow it down or make it faster. And because our speed variable is a Vector2, it will still be one after multiplying it with Time.time. So in the end we get a new Vector2 that will be used for the UV Offset.

Let's save the Script and then take a look at the Inspector again. Now we can modify the Speed. We will set the y speed (for vertical scrolling) to our wished speed and leave the x speed (horizontal scrolling) at 0 so it does not scroll horizontally at all:
UV Scroll in Inspector with Speed

The Scrolling Space Texture

If we press Play then we can now see our background scrolling downwards, just as if we were flying through space:
Background Scrolling

This effect is pretty good, but we will go one step further and implement Parallax Scrolling to add even more depth to it.

. . .

Premium Tutorial

Enjoyed this preview? Become a Premium member and access the full Unity 2D Space Shooter Game Tutorial!

All Tutorials. All Source Codes & Project Files. One time Payment.
Get Premium today!