Reader small image

You're reading from  Unity Game Development Essentials

Product typeBook
Published inOct 2009
Reading LevelIntermediate
PublisherPackt
ISBN-139781847198181
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Will Goldstone
Will Goldstone
author image
Will Goldstone

Will Goldstone is a longstanding member of the Unity community and works for Unity Technologies as a Technical Support Associate, handling educational content and developer support. With an MA in Creative Education, and many years experience as a lecturer in higher education, Will wrote the first ever Unity book, the original Unity Game Development Essentials, and also created the first ever video tutorials for the package. Through his sites http://www.unity3dstudent.com and http://learnunity3d.com Will helps to introduce new users to the growing community of developers discovering Unity every day.
Read more about Will Goldstone

Right arrow

Chapter 3. Player Characters

In this chapter, we'll expand the island scenario we created in the previous chapter by taking a look at the construction of the player character that you have already added to the scene. Stored as a prefab (a data template object) provided by Unity Technologies as part of the Standard Assets package, this object is an example of a first person perspective player character. But how does its combination of objects and components achieve this effect?

In this chapter, we'll take a look under the hood of this prefab, while looking at how each of the components work together to create our player character. You'll get your first look at scripting in Unity JavaScript. As we have already added our prefab to the game scene, it would be all too easy to continue with the development and accept that this object just works. Whenever you are implementing any externally created assets, you should make sure you understand how they work. Otherwise, if anything needs adjusting...

Working with the Inspector


As it is our first time dissecting an object in the Inspector, let's begin by looking at the features of the Inspector that are common to all of the objects.

At the top of the Inspector, you will see the name of the object that you have currently selected, along with a game object or prefab icon (red, green, and blue-sided cube or light blue cube respectively) and a checkbox to allow you to temporarily or permanently deactivate the object.

For example, when newly creating a game object (not from an existing prefab) with our Directional light, the top of the Inspector looks as follows:

Here, you can see the red, green, and blue icon, which represents a standard game object. It is also worth noting that the name box of this part of the Inspector can be used to rename an object simply by clicking and typing.

Below the icon, the active checkbox, and the name, you will see the Tag and Layer settings.

Tags

Tags are simply keywords that can be assigned to a game object....

Deconstructing the First Person Controller object


Let's begin by looking at the objects that make up our First Person Controller (FPC) before we look into the components that make it work.

Click on the gray arrow to the left of First Person Controller in the Hierarchy in order to reveal the objects nested underneath. When objects are nested in this way, we say that there is a parent-child relationship. In this example, First Person Controller is the parent, while Graphics and Main Camera are its child objects. In the Hierarchy, child objects are indented to show their parenting, as shown in the following screenshot:

Parent-child issues

When considering nested or child objects, you should note that there are some key rules to remember.

A child object's position and rotation values are relative to their parent. This is referred to as the local position and local rotation. For example, you may consider your parent object to exist at (500, 35, 500) in world coordinates, but when selecting a child...

Scripting basics


Scripting is one of the most crucial elements in becoming a games developer. While Unity is fantastic at allowing you to create games with minimal knowledge of game engine source code, you will still need to understand how to write code that commands the Unity engine. Code written for use with Unity draws upon a series of ready-built classes, which you should think of as libraries of instructions or behaviors. By writing scripts, you will create your own classes by drawing upon commands in the existing Unity engine.

In this book, we will primarily focus on writing scripts in Unity's JavaScript, as it is the easiest language to get started with and is the main focus of Unity's scripting reference documentation. While this book will introduce you to the basics of scripting, it is highly recommended that you read the Unity Scripting Reference in parallel to it, and this is available as part of your Unity installation, and also online at:

http://unity3d.com/support/documentation...

The FPSWalker script


In order to view any script in Unity, you will need to open it in the script editor. To do this, simply select the script in the Project panel, and then double-click on its icon to open it for editing.

On Mac, the default bundled script editor is called Unitron. On PC, the script editor is called Uniscite. These are standalone applications that simply allow you to edit various formats of text file, JavaScript and C# being two such examples.

There are other free text editors available that you may choose to use when writing scripts for Unity. You may set Unity to use a text editor of your choice. For the purposes of this book, we will refer to the default script editors Unitron and Uniscite.

Launching the script

Select the First Person Controller object in the Hierarchy, and then click on the name of the script file under the FPSWalker (Script) component in the Inspector so that it is highlighted in blue, as shown in the following screenshot:

This will highlight the location...

Summary


In this chapter, we've taken a look at the first interactive element in our game so far the First Person Controller. We have also taken a broad look at scripting for Unity games, an important first step that we will be building on throughout this book.

In the next chapter, you will begin to write your own scripts and look further into collision detection. To do this, we'll be returning to the outpost model asset we imported in Chapter 2, introducing it to our game scene, and making our player character interact with it using a combination of animation and scripting.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity Game Development Essentials
Published in: Oct 2009Publisher: PacktISBN-13: 9781847198181
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Will Goldstone

Will Goldstone is a longstanding member of the Unity community and works for Unity Technologies as a Technical Support Associate, handling educational content and developer support. With an MA in Creative Education, and many years experience as a lecturer in higher education, Will wrote the first ever Unity book, the original Unity Game Development Essentials, and also created the first ever video tutorials for the package. Through his sites http://www.unity3dstudent.com and http://learnunity3d.com Will helps to introduce new users to the growing community of developers discovering Unity every day.
Read more about Will Goldstone