Gigi Labs

Please follow Gigi Labs for the latest articles. Programmer's Ranch no longer has its domain, so please update your bookmarks and links to programmersranch.blogspot.com.

Friday, May 24, 2013

Unity3D: Moving an Object with Keyboard Input

Hi! :)

Today we're going to take a first look at the Unity3D game development engine. This engine is pretty easy to use and has gained a lot of popularity in recent years. It supports multiple languages (including C#) and multiple operating systems (including PC, Mac, Linux. Android, iOS, and others). Therefore, aside from Programming on Linux with MonoDevelop, this is one other way you can use C# on non-Windows platforms. You will learn in this article that Unity actually uses MonoDevelop anyway.

The first thing you will need to do is to download and install Unity from their website.

Once you have installed it and gone through the initial registration screens (you should normally opt to use the free version if you're just learning), you will be asked to open or create a new project:


In the "Create New Project" tab, select a folder where your project will be created. You do not need to import any packages for this tutorial.


You are then taken to the Unity interface, and shown a welcome screen. You can close the welcome screen. From the Layout drop-down menu at the top-right, select "Tall" to switch to a different layout.

Next, from the GameObject menu, select Create Other -> Cube to place a cube into the game world.


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 its properties (e.g. position in the game world) in the Inspector:


The Inspector shows information about the game object, such as its position in the game world. You can move it around by either changing values in the Inspector, or dragging the coloured arrows drawn over the object in the game world.

Underneath the Hierarchy section, you have a Project section. Right click on the Assets panel in the right-hand-side of the Project section and create a new C# script:


The script appears under Assets. Call the script "Movement". Then, double-click the script to edit it. When you do this, MonoDevelop (bundled with Unity) opens as the default editor:


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. Also, note that we are using different code from previous articles (such as "C#: ASCII Art Game (Part 1)"), because Unity has its own classes to handle this sort of stuff.

In MonoDevelop, press F8 to make sure that the code compiles. 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:


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:


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


Press the Play button again to stop the game. In MonoDevelop, 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.

Great! :) In this article, we learned about the Unity interface, and wrote a small script to move a cube in the game world. I have intentionally skipped certain details (e.g. navigation in the game world), as they will be covered in other articles. So check back later! :)

6 comments: