Reader small image

You're reading from  Getting Started with Unity 5

Product typeBook
Published inMay 2015
Publisher
ISBN-139781784398316
Edition1st Edition
Tools
Right arrow
Author (1)
Dr. Edward Lavieri
Dr. Edward Lavieri
author image
Dr. Edward Lavieri

Dr. Edward Lavieri is a veteran software engineer and developer with a strong academic background. He earned a Doctorate of Computer Science from Colorado Technical University, an MS in Management Information Systems (Bowie State University), an MS in Education (Capella University), and an MS in Operations Management (University of Arkansas). He has been creating and teaching computer science courses since 2002. Edward retired from the U.S. Navy as a Command Master Chief after 25 years of active service. He is the founder and creative director of three19, a software design and development studio. Edward has authored more than a dozen technology books, including several on Java.
Read more about Dr. Edward Lavieri

Right arrow

Chapter 4. Animating the Game Characters

So far, our game's design is complete, our game environment has been created, and we have imported game characters and other assets into our game. The game characters we added were the pig, piglet, baby chick, adult chicken, old farmer, and Colt, our young farmer. Our game characters would be boring if they just stood there like a rock. We'll breathe life into our characters and game by adding animations to each of the characters.

We will give each animal two animations so they can have some movement when they are idle, as well as when they eat. The old farmer will have talk and idle animations. The young farmer is our player-controlled character, so he will be able to walk, talk, run, take, and be idle.

After reading this chapter, you will:

  • Understand what Unity animations are

  • Understand what a player controller is

  • Be able to preview animations

  • Be able to animate the game's characters

  • Be able to create an animation clip

Animation basics


Animation is defined as a simulated movement, which is created by displaying a series of pictures or frames. In Unity, animations are displayed similar to the method used in movies, which involves displaying of a specific number of progressive frames each second. We use animations to bring our characters to life.

When a player uses arrow keys or another input device to move the game's main character around the screen, it is being animated. So, the game is responding to the user's input and playing the appropriate animation. For example, when the player holds the left arrow key down, the game's main character walks to the left. This is a walking animation in Unity.

Animations are typically created by using a 3D modeling tool such as Blender, 3D Studio Max, or Maya. As you will see later in this chapter, we can also create animations from within Unity.

For our game, we have two primary concerns regarding animation. First, we want to animate the young famer character so that the...

Character controllers


There are two types of player characters in games: those controlled by human players and those controlled by computers. The first type is known as a player character while the other type is referred to as a non-player character. Player characters can be almost anything you can imagine. In modern games, player characters are usually human or humanoid. This is not to say you cannot create a game in which the player character is an uprooted tree or a rock with seven legs. For our game, the player character will be a human boy named Colt.

Unity uses a game object called a character controller to enable user controls. There are two types of character controllers in Unity. The two types are first person controller and third person controller. The types refer to the user perspective, first or third, used during the game. Take a look at the following image that shows the player perspective:

With first person controllers, only part of the player's character is visible on screen...

Animating player characters


Our game, like most games, will consist of animated characters. Even if a non-player character only has a looped idle animation, it is important that they be animated. Otherwise, the characters will be more of a statue than a character.

Little farmer Colt

Let's continue working on our Colt character and ensure all of his animations work. By browsing the assets in the Project view, you can see there are five animations associated with Colt. The animations are idle, run, take, talk, and walk. These animations have already been created, so we are able to use them in our game.

Some of the animations will simply just seem to work because of the character controller we used. For example, if you test the game in play mode, you'll see that our walk animation is responsive to the navigation keys on our keyboard as well as the WASD keys. Also, although we did not create a jump animation, Colt already knows how to jump. You can test this by pressing the space bar while in game...

Previewing animations


When our game is being played, the characters will all have idle animations that play when the character is idle. The eat animations will be played when there is food to eat or water to drink. Colt's walk animation will be played in response to keyboard or mouse input. We'll add scripts that handle Colt's "take" animation, as well as the talk animations for the two farmers.

Unity provides us with the ability to preview animations without having to complete the game first and add our scripts. Our game has 16 animations, so being able to preview them without programming them into our game is a great time saver.

Let's preview Colt's animations. The steps are the same for all animations. In the following steps, we'll focus on the take animation:

  1. In the Project view, navigate and select Assets | Farmer Boy | FarmerBoy_Take.

  2. In the Inspector view, click on the Animations tab, as shown in the following screenshot:

  3. In the Inspector view, click and drag the expand section bar upward...

Creating animation clips


Animation clips are small animation sequences in Unity. They can be very small, isolated pieces of motion. For example, walking is a valid animation. Associated animation clips will be walk right, walk left, look up, look down, bounce, and more. Multiple animation clips can make up a single animation.

Unity has internal capabilities to create custom animation clips. This represents a great and relatively quick way of building an animation clip. Perform the following steps to create a short animation clip for our Colt character:

  1. Launch Unity.

  2. Open the Little Farmer Colt project and scene.

  3. In Hierarchy view, select the ThirdPersonController object.

  4. In the Scene view, zoom in as necessary so that you can clearly see the Colt character.

  5. Using the drop-down menu, navigate and select Window | Animation. This will bring up the Animation window. You'll see that the Colt character has a lot of animation clips listed in the window.

You'll notice in the top-left section of the Animation...

Summary


In this chapter, you learned how to add life to your game with the use of animations. You gained experience with first and third person controllers and learned about game player perspectives. This chapter guided you through the process of customizing the third person controller so our game would feature our little farmer character. You previewed animations and even created your own custom animation.

In the next chapter, you will be introduced to scripting with Unity. You'll learn how to use a tool called MonoDevelop and how to access your game's scripts. Specifically, we'll script our game so that, by the end of the chapter, you will be able to play a working version.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Getting Started with Unity 5
Published in: May 2015Publisher: ISBN-13: 9781784398316
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
Dr. Edward Lavieri

Dr. Edward Lavieri is a veteran software engineer and developer with a strong academic background. He earned a Doctorate of Computer Science from Colorado Technical University, an MS in Management Information Systems (Bowie State University), an MS in Education (Capella University), and an MS in Operations Management (University of Arkansas). He has been creating and teaching computer science courses since 2002. Edward retired from the U.S. Navy as a Command Master Chief after 25 years of active service. He is the founder and creative director of three19, a software design and development studio. Edward has authored more than a dozen technology books, including several on Java.
Read more about Dr. Edward Lavieri