This chapter is a general introduction to Unity3D, explaining the concepts of game engines and the general benefits that can be obtained from such a software. It presents some of the most interesting features of Unity3D, along with the novelties brought by Unity3D 4. In this chapter, we will look at Unity3D's interface, and how to include assets to scenes, using both built-in objects and primitives (for example, shapes, cameras, or lights) and external files such as 3D models, sounds, or textures. We will also become familiar with the most commonly used components in Unity3D. After completing this chapter we should be able to:
Become familiar with Unity3D's interface and its different windows and menus
Understand the different windows and layouts available in Unity3D
Understand the main differences between the Hierarchy, Project, Console, and Inspector windows
Navigate to the Scene view using shortcuts, create placeholders and duplicate objects in the Hierarchy window
Monitor messages displayed in the console
Understand and apply the concept of GameObjects
Differentiate, choose between, and combine different GameObjects and components
Apply transformations to objects (for example, moving, rotating, or scaling)
Add textures to objects
Add and use first- or third-person controllers
Unity3D is a game engine and makes it possible for indie game developers, hobbyists, and those new to programming, to design and develop video games, focusing essentially on the game mechanics, rather than the underlying layers necessary to build a game. Game engines usually provide an Integrated Development Environment (IDE), where all activities and tasks related to game development are seamlessly integrated using coding, objects, and environment creation. They usually make it possible for designers to control the logic of their game using high-level programming or scripting languages, hence decreasing the learning curve and improving the workflow. With the evolution of technology, many game designers have used game engines. While game engines were initially essential for the production of video games, they are now used for a wide range of applications, with purposes other than gaming. For example, game engines are now employed for simulation, teaching, and training, as they often make it possible to create and manage very realistic environments easily. Moreover, such tools provide means for the creation of universally accessible environments, thanks notably to popular export formats for web and mobile devices.
In addition to its built-in capabilities, Unity3D offers the possibility to employ third-party plugins that greatly enhance the workflow and add some very interesting effects and functionalities. We will have the opportunity to discover some of these libraries throughout this book.
Finally, Unity3D includes a built-in access to the assets store (https://www.assetstore.unity3d.com/), an online store that provides material for our Unity projects (for example, textures, characters, GUI systems, or scripts). While the majority of these items have to be purchased, some of them can be imported in our project for free, so that we can create a game with a small budget. Most of these items can be integrated seamlessly in our game.
As I am writing this book, Unity3D is in its 4th version. The current version is the fruit of a rapid and consistent evolution. Although each version offers significant novelties and functionalities, the main components and layouts are rather similar across all versions, which means that what we will learn in this book should still be relevant for subsequent versions of Unity3D.
Unity3D 4 includes a number of very exciting features such as Mecanim, a new system to animate both objects and characters, enabling users to retarget animations, apply state machines and smooth transitions between these (blend trees), and Inverse Kinematics (IK).
We can download Unity3D from the Unity3D website (http://unity3d.com/unity/download/).
Before we download it, it is a good idea to check the requirements to make sure that our system (that is, software and hardware) is up-to-date. We can visit http://unity3d.com/unity/system-requirements to check whether our system complies with the requirements. Once we have checked the requirements, we can download the latest version of Unity3D for either Mac OS (http://unity3d.com/unity/download/download-mac) or Windows (http://unity3d.com/unity/download/download-windows). Opening either of these URLs will start the download of Unity3D automatically.
While the latest version of Unity3D is available on the official website, it is also possible to download previous versions of Unity3D from the following link: http://unity3d.com/unity/download/archive. If we use this book as a support for a course, it may be the case that the college or university where we study may have a previous version installed in the labs, and we may want to work on the project we have created during class from home. Another reason for this is that when we open a Unity3D project with a new version of the software, we may not be able to reopen it with the previous version.
Once we have downloaded Unity3D, launch the installer.
Once the installation is complete, we can launch Unity3D. The first time Unity3D is launched, we may be required to register the software and to provide an e-mail address, so that we can receive frequent updates on Unity3D. Once the registration process is complete, we can then start using Unity3D. The first time we open Unity3D, a pop-up window labeled Welcome to Unity will appear. This window can provide us with useful links to tutorials, the assets store, and additional help on Unity3D. However, if we don't want to display this window every time Unity3D is launched, we may uncheck the box located at the bottom right hand corner of the window, as we can see in the following screenshot, and close the window:
By default, when we launch Unity3D for the first time, the project
AngryBots should be open. The default layout is applied in Unity3D, and you will notice that the screen is divided into several sections or views (as highlighted in the following screenshot), including the following:
The layout of this window can be modified using Window | Layouts. We can also resize the different windows and save our layout for a later use by navigating to Windows | Layout | Save.
The Scene view displays the scene and makes it possible to navigate through it. Scenes are comparable to game levels and include all relevant objects and environments. To the right of the tab Scene, we can find a tab labeled Game. This view displays the scene just the way it would appear when the game is launched (that is, from the active camera). For example, if we click on the Game tab, the view should switch to the camera used in the game to follow the player, as illustrated in the following image:
Unity3D provides several means and shortcuts to navigate through the scene, including the arrow keys from our keyboard. This mode is similar to the controls used in a First-Person Shooter (FPS), except that there is no mouselook option in this mode. Holding down the Shift key in this mode will increase our speed. To navigate in a mode that is closer to the controls usually found in FPS games, we can use the Fly mode, which is similar to the previous mode, except that it includes a mouselook feature. We can access this mode by holding down the mouse right button inside the scene. We can then navigate through the scene using the keys W, A, S, and D; float up and down using the keys Q and E, or look around by moving the mouse left, right, forward, or back. In addition to navigating through the scene, we can also focus on one object by double-clicking on this object in the Hierarchy window or by selecting this object in the hierarchy (by clicking on it once), and moving the mouse over the Scene view and pressing the F (focus) key (this will cause Unity3D to move the camera so that the object is displayed on the screen). We can zoom in and out by scrolling the mouse wheel; pan the view by clicking and dragging the mouse (note that panning the view works essentially when the hand tool is selected after pressing the Q key).
We can rotate the view by pressing the Alt key and clicking and dragging the mouse, or view the scene from the x, y, or z axes using the gizmo located in the top-right corner of the window, as illustrated in the following screenshot. Clicking on the y (green) arrow will display the view from the y-axis; the same applies to the z (blue), and x (red) axes. Clicking on the box in the middle of the gizmo will toggle the view between perspective and isometric modes.
There are many other shortcuts for Unity3D and we can find them in the documentation provided by Unity by selecting Help | Unity Manual from Unity3D and the pages dedicated to scene view navigation in Unity3D (http://docs.unity3d.com/Documentation/Manual/SceneViewNavigation.html). This being said, the shortcuts and controls described previously should be sufficient for us to start navigating through a Unity3D scene.
This view lists all objects present in the scene. By default, a camera is present and the scene is viewed through its lens. Any subsequent object created or imported will appear in this view (for example, light, camera, or box) as illustrated in the following screenshot:
Note that objects can be duplicated in this window using the keys Ctrl + D (or right-click + duplicate). Groups can be created in this window; for example, when a group of objects need to share the same position or orientation (for example, transform properties), an empty object can be created, and these objects can then be added to the empty object. This way, any transformation applied to the empty object (container or parent) will be recursively applied to the children.
For example, in the
AngryBots scene in the folder labeled
Environment (static) we will notice a container labeled
barrels that includes all barrels featured in the scene. If we apply a transformation to this container, this transformation will be applied to all objects included within this folder.
AngryBotsscene. If the
AngryBotsproject is open, this scene should be located inside the
Assetsfolder as illustrated in the following screenshot:
If, for some reason, you don't have the project
AngryBot, you can download it from http://unity3d.com/gallery/demos/demo-projects.
Navigate through the
AngryBotsscene using the different navigation modes explained earlier. Select some of the items and look at their features using the Inspector. Look at the scene from the x, y, and z axes. Open the Console window and look for any message in this window. Play the scene using the play icon located at the top of the window (that is, black triangle pointing right) or by pressing Ctrl + P.
To exit the play mode, we can either click on the play icon or use the shortcut Ctrl + P.
Select File | New Project. A window labeled Project Wizard should appear. This window provides a default directory for our project; however, we can set the location of our project by clicking on the Set button. By default, the name of the project is New Unity Project, but this can also be changed. At this stage, it is also possible to select and add the packages to include in our game. If we scroll down through the list below the label Import the following packages, we will see several packages, which consist of assets that can be used to enhance our game. These assets can be imported into our project at a later stage. Therefore, we will not select any of these for the time being.
Choose and set a directory and a name for our project (in the previous screenshot the project is called maze and is located in the directory
Once you are happy with the project directory and its name, click on Create Project.
After a few seconds, a new window should appear with our new project.
Now that we know how to navigate through the scene, we will learn how to create objects and add them to the scene. Unity3D makes it possible to add different types of built-in objects, including 3D primitives (for example, spheres, cubes, cylinders, or planes), lights (for example, point, directional, or area lights), Graphical User Interface (GUI) elements (for example, text or textures), or cameras. Each object can be added by selecting: Game Object | Create Other. Once an object has been created, we can change its properties using the Inspector or by directly moving, rotating, or scaling this object in the scene view. Once an object is selected, we can use the buttons located at the top-left corner of the scene view to apply transformations, as illustrated in the following screenshot:
The three buttons illustrated in the previous screenshot can be used to move, rotate, or scale the selected object, and can be accessed using the keys W, E, and R respectively. For example, if we select an object and click on the first button (or press the W key), three axes will appear on the object: a green axis (y), a blue axis (z), and a red axis (x). Dragging any of these axes will move the object in the corresponding direction. The same applies to rotating and scaling an object. Note that we can also constrain the object to a particular plan using the same technique but by dragging one of the colored plans that appear at the center of the object (for example, dragging the green box will move the objects in the horizontal x-z plane). For more information on the Unity3D interface, we can visit: http://docs.unity3d.com/Documentation/Manual/LearningtheInterface.html.
Select: Game Object | Create Other | Cube.
In the Hierarchy window, change the name of this cube from Cube to box1 (right-click on the object then select Rename from the contextual menu, or select the object in the hierarchy, left-click on the object, and type the new name). We can also rename an object by selecting this object in the hierarchy and by pressing Enter for Mac OS or F2 for Windows.
Make sure that the object is selected by clicking on it in the Hierarchy or in the Scene view.
In the Inspector window, change the x, y, and z position parameters of this object to (x=0, y=0, z=0).
Double-click on this object in the hierarchy to focus the camera on it.
Add a color to this box:
Our box looks pretty dull, and it would be great to add a color to it. This can be achieved by creating and applying a material (for example, color or texture) to the box.
From the Project window, select Create | Material as shown in the following screenshot:
This will create a new material labeled New Material in the Assets window.
Change the name of this new material to red (right-click on the object and select Rename from the contextual menu, or left-click on the object).
Make sure that this material is selected, and look at its properties in the Inspector window.
This should open a window labeled Color. This window makes it possible to pick a color for this material as shown in the following screenshot:
Pick a red color (for example, click on a red area) and close the Color window. We should see that the Preview of the material now shows a red sphere as shown in the following screenshot:
Apply the material to the box; this can be done in at least two ways.
The second way is as follows:
Click on the object box1.
Look at the Inspector window and click on the Materials attribute of the Mesh Renderer component for this object as illustrated in the following screenshot:
Click on the circle to the right of the label Default-Diffuse so that we can change the material. A new window will appear.
From the new window, click on the tab labeled Assets and type the text
redin the search field located at the top of this window as shown in the following screenshot:
This should return one result, which is the material we have just created. Click on the material red and close the window. The cube should turn to red.
It is also possible to apply a texture to an object by dragging-and-dropping the texture on the object.
Add a light of our choice to the scene as shown in the following steps:
In the Inspector window, change the position of this light to (x=0, y=4, z=0).
In the Inspector window, change the rotation parameters of this object to (x=90, y=0, z=0). This will rotate the light around the x axis, so that it points downwards (that is, toward the cube).
Any of these properties (for example, position or rotation) can be amended in the Inspector window by either entering the value in the text field, or by dragging the parameter we need to modify. For example, to change the x value of the position, we could position our mouse over the x label (the cursor will then turn into a double arrow) in the Inspector window and then drag-and-drop it to the right (to increase the value) or to the left (to decrease the value).
Move the camera so that the objects can be seen from the camera:
The camera is already present in the Scene and is labeled as Main Camera by default.
Select this camera (from the Hierarchy or Scene view).
In the Inspector window, change the position of this camera to (x=0, y=4, z=0).
In the Inspector window, change the rotation of this camera to (x=90, y=0, z=0). This will, as it was done for the light, rotate the camera around the x axis, so that it points downwards (that is, toward the cube). We can check the camera by looking at the camera view (that is, the rectangle located at the bottom-right corner of the Scene view), or by clicking on the Game tab (to display the game view).
So far, we have learned how to include objects and apply colors; however, for more realism, it would be better to use textures instead. Thankfully, adding textures is relatively easy in Unity3D. Before we can add a texture, we need to identify and acquire a texture. For many game programmers who need to create a prototype quickly and who prefer to focus on the mechanics of the game, it is often more convenient to use free online assets for games. For our first level, we will be using one of these resources:
When looking for free assets and textures, you can visit: http://wiki.unity3d.com/index.php?title=Free_Game_Content.
This wiki includes links to textures, models, music, sound effects, and fonts.
For our textures, we will use the site www.cgtextures.com as follows:
Launch your web browser and open the page http://cgtextures.com.
In order to use textures from this website, we need to create an account and register. If you already have an account on www.cgtextures.com, just log in (Members | Login); otherwise, you can create an account (Members | Free Account). The registration process should be relatively short. Once it is complete, use your new user name and password to log in (Members | Login).
On the left-hand side of the window, we can enter keywords to look for particular textures.
Click on the image returned from the search (in the right frame). A new window should now appear, displaying eight different textures.
Click on the first texture (image1: 640x640); this will download the texture to our computer (
Create a new folder within this folder (from the Project window, select Create | Folder).
Rename the new folder
Select this folder (that is, click on this folder once).
Select: Assets | Import New Asset.
Browse to the location where the texture was saved on our computer (for example, the
Select the texture (
PlywoodNew0046_1_S.jpg) and click on Import.
An asset labeled with the name of the texture (PlywoodNew0046_1_S) should now appear in the folder
In the Hierarchy view, duplicate the game object labeled
Call this new object
Change this object's position to (x=4, y=0, z=0).
Drag-and-drop the plywood texture that we have just imported to the object
box2either in the Scene view or in the Hierarchy view.
The object labeled
box2should now feature a wooden texture.
Unity3D also makes it possible to import 3D objects from many different formats, including
.obj. We can import objects by selecting Assets | Import New Asset. Both static and animated objects can be imported and animated (when applicable). For conversion to/from these formats, we can visit the Unity3D page dedicated to 3D object imports: http://docs.unity3d.com/Documentation/Manual/HOWTO-importObject.html.
In some cases, we may need to navigate through the game using a first- or third-person view. This requires using a camera and the ability to move it based on the players' keyboard entries. Thankfully, Unity3D includes built-in objects to implement both types of navigation. If we have chosen to import these assets when creating our project (see previous sections), these assets are located in the Project view, inside the folder
Standard Assets, and are named 3rd Person Controller and First Person Controller. If you haven't done so yet, we will need to import the character controller package as follows:
Select: Assets | Import Package | Character Controller.
A new window labeled Importing package will appear.
Click on the button labeled Import.
This will import the character controllers in our project as illustrated in the following screenshot:
Once we have imported this package, both controllers appear with a blue box to their left. This indicates that they are prefabs. Prefabs are comparable to templates, and make it possible to reproduce similar objects based on the same template, without the need to recreate them. Once a template is created and instantiated, if the prefab is modified, all instances will also be modified accordingly, thus saving time of the game developer. We will look at the concept of prefabs in the next chapters.
If we click on any of these prefabs, we will see in the Inspector window that they include a set of components and attributes such as gravity, walk speed, or run speed, which can be modified and affect the behavior of the controller accordingly.
Create a new box (Game Object | Create Other | Cube).
Rename this box
Scale-up this cube by 20 on the x and z axes:
Select the cube labeled
In the Inspector window, change the position of this object to (x=0, y=-1, z=0) and its scale properties to (x=20, y=1, z=20). This will scale the cube on the x and z axes. Apply a texture to this cube.
Open the site www.cgtextures.com.
Search a tile texture using the keywords
Click on the image returned from the search. A new window should now appear, displaying three different textures.
Click on the first texture (Image1: 700x700); this will download the texture to your hard drive (
In Unity3D, select the folder
chapter1in the Project window, so that the image can be imported in this folder.
Import this texture as explained in the previous section (for example, Assets | Import new Asset).
Apply the texture to the object labeled
Click on the object labeled
floorand look at its properties in the Inspector window.
By importing these two images, Unity3D has automatically created the corresponding textures that will be used in the project. These textures are located in a folder called
material, which is within the folder
chapter1 that was created previously.
Let's change the tiling properties of the texture used for the floor:
Click once on the object
floorin the Hierarchy window.
In the Inspector window, click once on the component labeled TilesPlain0072_5_S, this should display more properties for this texture as illustrated in the following screenshot:
Change the tiling properties to (x=20, y=20).
Add First Person Controller to the scene:
Drag-and-drop the First Person Controller prefab (that is, blue box labeled First Person Controller) by selecting Standard Assets | Character Controllers onto the Scene view (or the Project view).
Move this First Person Controller to the position (x=0, y=0.6, z=-5) using the position properties in the Inspector window. This should place First Person Controller slightly above the ground; if it is too low, the collision may not be detected and the controller will fall indefinitely.
Test the scene:
First, let's deactivate the third-person controller:
The scene already includes a First Person Controller. To avoid any conflict between these, it is better to deactivate the First Person Controller.
Select First Person Controller in our scene, and uncheck the box to the right of the label First Person Controller in the Inspector window as highlighted on the following screenshot:
This will deactivate this object, which means that it will still be present in the project, but not used or seen when the scene is played until it is reactivated.
Add the third-person controller:
Open the folder
Character Controllers, which is located in the folder
Standard Assets. This folder should include two prefabs symbolized by a blue cube (3rd Person Controller and First Person Controller) and one folder (
Drag-and-drop 3rd Person Controller onto the Scene view (or the Project view).
Test the scene:
Press the Play button located at the top of the window or (Ctrl + P).
Navigate through our scene using the keys W, A, S, D, or the arrow keys as illustrated in the following screenshot:
Quit the Play mode by clicking on the Play button or use the corresponding shortcut (Ctrl + P).
Save our scene (File | Save Scene As) and name it
In this chapter, you have learned about Unity3D. We have looked at the user interface employed in Unity3D, the different windows available, as well as useful shortcuts. Then, we have learned how to create and manipulate built-in objects, using both the inspector and contextual commands and menus. We have also learned how to apply colors and textures. Building on these skills, we then added both first- and third-person controllers for the player to navigate through the scene.
In the next chapter, we will combine the skills that we have acquired so far to create an indoor environment for our game including walls, doors, and water.