patreon

 

 

About this project

Skill level 1
Time to complete 30 minutes

New concepts

  1. Adding sprites to a game
  2. Rigidbody2d
  3. Kinematic objects
  4. Detecting key presses
  5. Moving objects

Assumed previous experience

Fire up Unity and log in to your account. Click New to start a new Unity project. Name your project Controllable Spaceship and Select the 2d option.

If you don’t select 2d, the following instructions won’t work.

Now click the Create Project button and you will have an empty project that looks something like this next image

UNity 2d after starting a new project

UNity 2d after starting a new project

The first thing we need is a graphic image to represent our spaceship. You can download the image below or provide your own.

The Space Invaders player ship graphic

 

Next, we need to import the image into our Unity project. There are a few different menu options that allow us to do this. The quickest and simplest is to right-click the Project window and choose Import New Asset…

Unity-2d-import-new-asset

Importing a sprite into Unity

As an alternative to the previous step you could also use the main menu and select Assets | Import New Asset… or just drag and drop the graphic file into the Project window.

Browse to the graphic you will be using and import it. Unity knows that we are making a 2d game and the graphic is imported as the appropriate asset type, a sprite. To actually add the sprite to the game scene, drag and drop it from the Project window to the Hierarchy window. The spaceship is in the Game view as shown next.

Unity-2d-spaceship-in-game-view

A sprite in the unity Game view

Single left-click the playership item in the Hierarchy window (not the Project window), to make sure it is selected. Now we can turn our attention to the Inspector window. The next image shows how the Inspector window should look at the moment.

Unity-2d-sprite-in-inspector-window

A sprite in the Unity Object inspector window

The Inspector window shows you the details of the currently selected game object, in our case, this is the sprite. If we are going to control our sprite, we need to make it into something a bit more advanced. We will do so by adding components to it.

We want our spaceship to be part of the physics system. This is so it can move and be collided with (Although we won’t be doing collisions in this tutorial). We need to add a RigidBody2d component.

Click the Add Component button in the Inspector window. You can see this button at the bottom in the image above. Now select Physics 2d | RigidBody 2d. Doing this step has an immediate effect on our spaceship. The RigidBody2d component has added our spaceship to the Unity 2d physics engine. Run the game by clicking the play button in the top center of Unity as shown below.

Unity-2d-run-the-game

Selecting the playership game object

Watch as the spaceship falls into oblivion because of the gravity in the physics system.

Unity-2d-deafult-gravity

The playership object falls and disappears off the bottom of the screen

It’s great to have a bit of life in our game, but obviously, most spaceships don’t behave like this. We must have a RigidBody 2d component if we want to move the spaceship but at the same time, we don’t want the spaceship falling out of the game. We can fix this by making our RigidBody 2d, kinematic. Select the Is Kinematic option for the RigidBody 2d component in the Inspector window.

Unity-rigidbody-2d-is-kinematic-setting

Making an object kinematic

Kinematic means that we can exert physics forces on it to move it but it will not be subjected to all the physics forces of the system, like gravity. If you run the game again you will see that the spaceship doesn’t fall into oblivion anymore.

Now we can make the spaceship move. To do so we will need to do some C# coding. We will add a new C# script to the game or to be more specific, we will add a new C# script to the spaceship object.

Make sure the spaceship object is selected by single left-clicking it in the Hierarchy window. Next, in the Object Inspector window, click the Add Component button. Choose New Script and name it SpaceshipControl. This step is shown in the next image.

Unity-2d-adding-new-csharp-script

Adding a new script to a game object

Now click Create and Add. Notice there is a new component in the Object Inspector. Click the little gear icon in the new scripting component, select Edit Script and we can start coding. This step is pictured next.

Unity-2d-editing-new-csharp-script

Adding a new C# script to the game object

You could also double-click SpaceshipControl from the Project window.

This is what the new MonoDevelop window will look like and we can now add the code to control the spaceship.

Unity-2d-new-csharp-script-in-monodevelop

The default C# script

Edit the code in MonoDevelop to look like this next code. Note that you only need to add three lines but in the code below, some of the lines wrap onto multiple lines for the purpose of presentation. You must also add them in exactly the right place.

The first thing we do in the code is add a float called speed. This is the number of units we want to move each second. Next, we add an Vector3 object called move. This Vector3 holds a value for x, y and z which we will use to eventually hold the value to move in each direction. As this is a 2d game, z will always be zero.

If the concept of an object is confusing take a look at the tutorial Object oriented programming in C#.

In the update method we set the x and y values of move based on the values returned from Input.GetAxis("Horizontal") and Input.GetAxis("Vertical"), respectively. Input.GetAxis detects whether the cursor (arrow keys) is currently pressed.

The final line of code that we added alters the position of the spaceship. transform.position is another Vector3 and by adding the move Vector3, speed and Time.deltaTime to it, the spaceship will move exactly the correct amount, in each frame, based on how long the frame took.

Play the game and you can move the spaceship all over the place (including off the screen) with the cursor keys.

Unity-2d-controlling-a-spaceship-with-the-arrow-keys

I can fly!

Try increasing the value that the speed variable is initialized to and playing again.

Obviously, a lone spaceship flying around an empty screen isn’t going to get the green light on Steam. In the next tutorial, we will learn how to spawn new objects into our Unity game.

patreon