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#, JavaScript ,Boo) .
1) The first thing you will need to do is to download and install Unity from their website.
2) Once you have installed it, you will be asked to open or create a new project:
3) In the "Create New Project" tab, select a folder where your project will be created.
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.
4) Next, from the Game Object 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.
5) 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:
6) 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:
7) In the Update() method, add the following code:
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Vector3 position = this.transform.position;
position.x--;
this.transform.position = position;
}
8) 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.
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:
9) 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:
10) 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. Thanks
1 Comment(s)