3iX

Reliable $1 Web Hosting by 3iX

Wednesday, February 3, 2010

3D Platformer Tutorial
Building a 3D Platform Game in Unity 2.0

Introduction
With its myriad features, including heightmapped
terrains, native networking support,
complete physics integration and scripting, Unity can be daunting for newcomers,
but mastering its many tools is infinitely rewarding.
This tutorial will walk you through the process of building a complete 3D platform
game level with a thirdperson
perspective view. This includes everything from player
controls, collision detection, some advanced scripting, blob shadows, basic AI, adding
a game HUD, cutscenes
and audio spot effects.
What you will learn
This tutorial focuses on the technical side of building a game in Unity, covering the
following:
• Character Controllers
• Projectors
• Audio Listeners, Audio Sources & Audio Clips
• Multiple Cameras (and how to switch between them)
• UnityGUI scripting system
• Colliders
• Messages & events
Unity is a powerful tool for game
development, suitable for many
game genres, from firstperson
shooters to puzzle games.
• Lighting
• Particle systems
• Blob shadows
• Scripting (AI, state machines, player controls)
This tutorial will show how these features can be used together to create a game.
What you should already know
This tutorial makes extensive use of scripting so you should be familiar with at least
one of the supported scripting languages: JavaScript, C# or Boo. (JavaScript is used for
the scripts in this tutorial.)
It is also assumed that you are familiar with Unity’s interface and know how to perform
basic operations, such as positioning an asset in a scene, adding Components to a
GameObject, and editing properties in the Inspector.
Project Organization
Unity does not attempt to force a particular way of organizing your project's assets.
You may prefer organizing your assets by asset type, with separate folders for, say,
"Textures", "Models", "Sound effects" and so on. At Unity Technologies, we have
found this works well for smaller projects. For more complex projects, our users generally
recommend organizing assets by function, perhaps grouping them under folders
such as "Player", "Enemies", "Props", "Scenery", and so on.
This tutorial’s project was worked on by a number of team members and grew organically
to reflect their different conventions and styles. In the interests of authenticity,
we have decided to leave the project's organization as it was as this is more representative
of a 'smaller' project's organization and structure.
Abstract GameObjects & Components
Unity's design places each scene's assets at the center of the development process. This
makes for a very visual approach to game development, with most of the work involving
dragging and dropping. This is ideal for the bulk of level design work, but not all
assets can be displayed in this way. Some assets are abstract rather than visual objects,
so they are either represented by abstract icons and wireframe gizmos e.
g. Audio
Sources and Lights or
are not displayed at all within the Scene View. Scripts fall into
this latter category.
Scripts define how assets and GameObjects in a Unity Scene interact with each other
and this interactivity is at the core of all games. For this reason, it is usually a good
plan to keep informative notes inside your scripts.
This tutorial will assume you can read the provided scripts and understand the many
comments liberally sprinkled throughout them. However, when a particular scripting
technique or concept is important, we will cover it in detail.
6
The scripts are documented through the use of extensive comments and have also
been designed to be as selfexplanatory
as possible in their design. We encourage you
to read through the scripts as we introduce them in this tutorial, studying their workings.
Feel free to experiment!
Files
The most uptodate
files for this project can be downloaded from:
http://unity3d.com/support/resources/tutorials/3DPlatformProject.zip
The “Scenes” folder contains the final result: a main menu Scene, a game over Scene
and a Scene containing the completed game level.
This tutorial assumes you already know basic Unity controls, such as positioning objects
in a scene, so the first starting point Scene already has the basic scenery and some
props in place.
Typographical Conventions
This is a long tutorial containing a lot of information. To make it easier to follow,
some simple conventions are used:
Background & Tangents
Text in boxes like these contains additional information that may help clarify the main
text.
Scripting code will appear as shown below:
// This is some script code.
Function Update()
{
DoSomething();
}
NOTE The scripts included in the tutorial include plenty of comments and are designed
to be easy to follow. These comments are usually omitted in the code
fragments in the tutorial text to save space.
Actions you need to perform within Unity are shown like this:
Click on this;
Then this;
Then click Play.
7
Script names, assets, menu items or Inspector Properties are shown in boldface text.
Conversely, a monospace font is used for script functions and event names, such as
the Update() function in the script example above.
Unity Conventions
Unity is a unique development system. Most developers will be used to working in a
code editor, spending 90% of their time editing code and even writing code to load
up and use assets. Unity is different: It is assetcentric
rather than codecentric,
placing
the focus on the assets in much the same way as a 3D modeling application. For this
reason, it is worth understanding the key conventions and terminology unique to
Unity development:
Projects
A game built in Unity will consist of a Project. This contains all your project’s elements,
such as models, scripts, levels, menus, etc. Usually, a single Project file will contain all
the elements for your game. When you start Unity 2, the first thing it does is open a
Project file. (If you have only just installed it, this will be the Project file containing the
Island Demo.)
Scenes
Each Project contains one or more documents called Scenes. A single Scene will contain
a single game level, but major userinterface
elements, such as game menus,
gameover
sequences or major cutscenes
may also live in their own Scene files. Complex
games may even use entire Scenes just for initialization purposes. Thus all levels
in a game will most likely be Scenes, but not every Scene will necessarily be a game
level.
GameObjects, Components, Assets & Prefabs
Key to understanding Unity is the relationship between a GameObject and a Component.
GameObjects
A GameObject is the fundamental building block in Unity. A GameObject is a container
for different pieces of functionality called Components. A GameObject almost
always contains more than one Component. All GameObjects contain a Transform
Component, which defines its position and orientation.
GameObject Hierarchies
The real power of the GameObject is its ability to contain other GameObjects, acting
much like a folder in OS X’s Finder. This allows hierarchical organization of GameObjects,
so a complex model or a complete lighting rig can be defined under a single
parent GameObject. (In fact, most models will appear in Unity as a hierarchy of
GameObjects because this reflects how they are defined in the modeling package.) A
GameObject defined inside another GameObject is considered a child GameObject.
8
Components
Components are the building blocks of GameObjects. Without them, the GameObject
won’t do anything interesting.
A Component may represent visible entities, such as meshes, materials, terrain data or
a particle system. Other Component types are more abstract, such as Cameras and
Lights, which do not have a physical model representing them; instead, you will see an
icon and some wireframe
guidelines illustrating their key settings.
A Component is always attached to a GameObject; it cannot live alone. Multiple Components
can be attached to the same GameObject. GameObjects can support multiple
Components of certain types―a GameObject can contain any number of scripts, for
example. But others, such as those used to define particle systems, are exclusive and
can only appear once in any single GameObject. For example, if you want to define
multiple particle systems, you would normally use a hierarchy of GameObjects, each
containing its own set of particle system Components.
Assets
All your imported Assets appear in the Project Pane and they can be almost anything:
a simple material or texture, audio files, or even a complete, prefabricated GameObject
(known as a “Prefab”).
For example, a player character Prefab could be defined as a single Asset, containing
the model and its associated animations. It could also contain script Components,
audio clips and any other Components it needs to function, so you could simply drag it
into a Scene and instantly have a fully operational avatar.
Custom Icons & Gizmos
You can tell Unity to display custom icons and other visual information for your Assets
if you wish. We will see an example of this in the next chapter.
Your project’s Assets are shown in the Project Pane. When you drop one into your
Scene, it appears in the Hierarchy Pane, which defines the content of the Scene. (A
Scene is the equivalent of the stage in a theater. It can be a level, a menu, a multiplayer
game lobby whatever
you wish.) The Project Pane is retained across all Scenes
in your Project.
Prefabs
A Prefab is an Asset which has been defined as a template. It is to Unity what a template
document is to a word processing application. When you place a Prefab into
your Scene, Unity places a link to the Prefab into the Hierarchy Pane, not a complete
copy. This is called instantiation. Each link you make is referred to as an instance of
the Prefab.
If you click on a Prefab in your Project Pane and tweak its settings, you will find that
those changes are instantly reflected in all the instances in your Scene. This makes Pre9
fabs ideal for many reusable
elements, such as bullets, enemies and so on. If you find
your enemy isn’t behaving correctly, you only need to adjust the script or settings in
the original Prefab instead of editing each one in the Scene individually.
However, if you need to adjust a couple of settings in a specific instance of a Prefab,
you may do this too: these changes will only affect that particular instance.
Prefabs are displayed in blue text in both the Project and Hierarchy Pane.
NOTE A Prefab instance cannot have additional Components added to it as doing so
will break the link to the original Prefab. Unity will warn you if you try and do
this. Unity will, however, allow you to update the original Prefab with such
changes after the link is broken.
Acknowledgments
This tutorial could not have been produced without the following people:
David Helgason, Joachim Ante, Tom Higgins, Sam Kalman, Keli Hlodversson, Nicholas
Francis, Aras Pranckevičius, Forest Johnson and, of course, Ethan Vosburgh who produced
the beautiful assets for this tutorial.

No comments:

Post a Comment

News