Unity3D: Moving an Object with Keyboard Input

This is an updated version of the article originally posted on 24th May 2013 at Programmer’s Ranch. The original article used Unity 4.1.3f3, MonoDevelop, and 3D settings (2D in Unity didn’t exist back then), on Windows XP. This updated article uses Unity 5.2.2f1, Visual Studio 2015, and 2D settings, on Windows 10. Parts of the article have been rewritten, and syntax highlighting has been added.

The Unity3D game development engine has gained a lot of popularity in recent years. It supports various target operating systems, can be scripted in various languages, has a large community, and is always evolving. In this article, we’ll see how we can set up a simple project and have the player move an object by pressing the arrow keys on the keyboard.

The first thing you will need to do is to download and install Unity from their website. Once you’ve installed it and gone through the initial registration screens, you can opt to create a new project:

unity3d-input-startup

Click on the “New” button (shown in the image above). Here, you can choose the name and location of your new project, as well as whether to use 3D or 2D settings. For this particular article it doesn’t matter; the original article used 3D settings, while for this updated version I’ve done everything using 2D settings.

unity3d-input-newproject

Once you click “Create project“, your project will open in the Unity IDE.

From the GameObject menu, select 3D Object -> Cube to place a cube into the game world:

unity3d-input-create-cube

The cube will now be listed in the Hierarchy section – this section shows you a list of objects in your game world. You can also see the objects in the game world itself, in the left portion of the screen. If you click on the cube, you can see and manipulate its properties (e.g. position in the game world) in the Inspector:

unity3d-input-cube-properties

Right click on the Assets panel inside the Project section, and create a new C# script:

unity3d-input-create-cs-script

The script appears under Assets. Call the script “Movement“. Then, double-click the script to edit it. This will launch an external editor, probably MonoDevelop or Visual Studio:

unity3d-input-visual-studio

In the Update() method, add the following code:

if (Input.GetKeyDown(KeyCode.LeftArrow))
{
    Vector3 position = this.transform.position;
    position.x--;
    this.transform.position = position;
}

What are we doing here? In Unity, you can attach scripts to objects in order to make them do stuff. In our case, we will attach this script to the cube to be able to move it with the arrow keys.

The “this” in a Unity script refers to the object that the script is attached to (e.g. the cube). Each object has a transform property, which contains stuff like its position in the world. The position consists of a Vector3 which contains the x-, y- and z-coordinates of the object in the world.

The code above simply makes the object move left when the left arrow key is pressed, by varying the x-coordinate of the object’s position. While it would normally make sense to modify x directly, you can’t do that in Unity.

In the external editor, build the project to make sure it compiles (F8 in MonoDevelop, or Ctrl+Shift+B in Visual Studio). Then, switch back to Unity. Drag the Movement script onto the Cube object in the Hierarchy section. Once you click on the Cube in the Hierarchy section, you should see the Movement script in the Inspector:

unity3d-input-attached-script

Now, Press the Play button at the top of the Unity interface to start playing. The world view changes into an interactive rendering of the game:

unity3d-input-run-start

Test the game by pressing the Left Arrow key. Doing this should move the cube to the left:

unity3d-input-run-moved

Press the Play button again to stop the game. In the external editor, update the code to handle movement in all four directions:

if (Input.GetKeyDown(KeyCode.LeftArrow))
{
    Vector3 position = this.transform.position;
    position.x--;
    this.transform.position = position;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
    Vector3 position = this.transform.position;
    position.x++;
    this.transform.position = position;
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
    Vector3 position = this.transform.position;
    position.y++;
    this.transform.position = position;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
    Vector3 position = this.transform.position;
    position.y--;
    this.transform.position = position;
}

Press Play again in Unity to test it. Note that the cube returned to its original position. That is because each time you press Play, a new session is started with all objects having their default values.

So in this article, we have learned a few things about the Unity IDE, and we wrote a small script to move a cube in the game world. The intention was to get something working as quickly as possible, giving the reader a feel of working with Unity3D, while leaving details for other articles.

11 thoughts on “Unity3D: Moving an Object with Keyboard Input”

    1. Easy, just add an if condition before changing the transform position. Similar to how it’s done in this article, but instead, you want to change the transform position only if the object is still within the boundary.

  1. This code is just what I was looking for, however when using it as it is the cube moves in 1 unit increments. How can I make the increments smaller? Say moving by 0.01 unity units? Thanks

      1. Im new to coding…Im having the same issue…I need it to move in smaller amounts (like 0.02), where would I put that?

        1. Replace ++ with += 0.02f. Likewise, replace — with -= 0.02f. It’s a good idea to become familiar with the fundamentals of the programming language before attempting to build something.

  2. Right this might sound mad yeh but I have litrally spent the last 3 hours trying to get a cupboard to open pmsl.
    Ive tried loads of stuff a lot of people using animation but I don’t want to I just wanna use script I got doors to open but that was transform.rotation I can not get the drawer to open at all need help, im using raycast with E but fa is working.

Leave a Reply

Your email address will not be published. Required fields are marked *