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.
Now click the Create Project button and you will have an empty project that looks something like this next image
The first thing we need is a graphic image to represent our spaceship. You can download the image below or provide your own.
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…
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.
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.
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.
Watch as the spaceship falls into oblivion because of the gravity in the physics system.
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.
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.
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.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using UnityEngine; using System.Collections; public class SpaceshipControl : MonoBehaviour { float speed = 10f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { // What is the player doing with the controls? Vector3 move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0); // Update the ships position each frame transform.position += move * speed * Time.deltaTime; } } |
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.
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.
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.
Am having issue concerning the input axis horizontal setup
Hi there. Feel free to ask a question or give some details and I will try and assist.
Question what about for a 2d rag doll character? I assembled one out of multiple parts and I can’t find a way to control it
I have never done this except in Unity. This closest I have done in Android API is simple sprite sheet animation. http://gamecodeschool.com/android/coding-android-sprite-sheet-animations/
The kinematic feature (as far as I’m aware, though I know I’m very new to navigating this and absolutely don’t mean to waste anyone’s time)- was removed. I’ve made due by turning the gravity scale of the playership object to 0, thought I don’t know enough to understand the difference entirely. At the moment though, doing this preserves the intended function in this tutorial.
Thanks for taking the trouble to post this. I am way out of touch with Unity at the moment and this could help people.
The ‘Kinematic’ feature is now an option on the ‘Body Type’ dropdown, rather than a checkbox now. It’s not great when they change the interface like that as it instantly de-values tutorials that people have created and kindly made available. But on the other hand it may well be a better way to organise those options now, so it’s unfortunate, but at least they didn’t change it from Kinematic to some other term.
I cannot move with this code. I need to add Rigidbody2D rigBody below the float speed = 10f;
and
rigBody = GetComponent (); inside void Start
I am not sure how to help.
Great article! Thanks my character finally moves. Thanks for making this tutorial, it really helped .