Reader small image

You're reading from  Unity 5.x Animation Cookbook

Product typeBook
Published inMay 2016
Reading LevelExpert
PublisherPackt
ISBN-139781785883910
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Maciej Szczesnik
Maciej Szczesnik
author image
Maciej Szczesnik

Not interested. Too busy with current game project. Source: Linkedin.
Read more about Maciej Szczesnik

Right arrow

Chapter 5. Character Actions and Expressions

This chapter explains the usage of animations for character actions and expressions and covers the following recipes:

  • Creating an appear or a disappear animation
  • Creating background characters and critters with animation-driven behavior
  • Using Blend Trees to create randomized actions
  • Using Quaternion.LookRotation() and Animator.SetLookAtPosition() methods to make characters follow an object with their gaze
  • Action Points - performing an action in a specified spot
  • Synchronizing an animation with objects in the scene
  • Using IK for interacting with scene objects
  • Animating facial expressions with Blend Shapes

Introduction


In most games, characters perform a variety of actions and expressions. This chapter explains how to use animations to create believable behaviors and interactions.

Creating an appear or a disappear animation


If our game has monsters in it, there is a huge probability that we will need to use an appear or a disappear animation to introduce such characters in the scene. Often times, monsters spawn near our players to ambush them. This is where the appear animations come in handy.

Getting ready

Before we start, you should have a character with at least two animations: a looped Idle animation and an Appear animation. This animation should start below the ground (or high in the air if your character is a flying one). It should end with the Idle pose. See the following image for reference:

Appear animation key frames (the animation is done "in place")

You can download the provided example; open the project in Unity and go to the Chapter 05 Character actions and expressions\Recipe 01 Creating an appear or disappear animation folder. You will find a scene called Example. Scene there. When you play it, a Spider character will be spawned (instantiated) and it will...

Creating background characters and critters with animation-driven behavior


In this recipe, we are going to createanimated characters that will serve as decorations in the game. Such characters' behavior is driven only by animations; thus, we can have quite a large number of them in the game.

Getting ready

Before we start, we need to have a character with a few animations. In this example, we are using a bird. It has IdleStartFlyingFlyingInCircles, and Land animations. The Idle animation is looped and played when our birds sits on the ground. The StartFlying animation is a transition between Idle and FlyingInCircles, which is a looped animation of the bird flying around. The last animation, the Land animation, is a transition between FlyingInCircles and Idle. We don't use root motion for those animations because we want the birds to always land in the same position (we don't want to check where the ground is, it's just a decoration).

You can also download the provided example Unity project...

Using Blend Trees to create randomized actions


In this recipe, we will create a simple randomized action using a Blend Tree.

Getting ready

Before we start, we need to have a character with at least two different animations for the randomization to work. In this example, we are using a character with the WalkIdleHurrayWave, and PickUp animations. The Walk and Idle animations are used as helpers. The three others are being randomized. You can also use the example project; go to the Chapter 05 Character actions and expressions\Recipe 03 Using blend trees to create randomized actions directory. You can find the Example.scene scene there, with two characters. You can start the game to see the effect. Both characters should play different animation after walking. You may need to start the game several times because we have only three animations to pick from, so there is a 9 percent chance that both characters will play the same animation. In the Rigs directory, you can find the required animations...

Using Quaternion.LookRotation() and Animator.SetLookAtPosition() methods to make characters follow an object with their gaze


Sometimes you need a character to look at an object in the game, for instance, at the camera. To do so, we can use two methods: Quaternion.LookRotation() and Animator.SetLookAtPosition(). We will cover both in this recipe (the second one is covered in the There's more... section).

Getting ready

To follow this recipe, you need a character with one Idle animation. You can also go to the Chapter 05 Character actions and expressions\Recipe 04 Using LookRotation and SetLookAtPosition methods to make characters follow an object with their gaze directory. Open the Example.scene scene there. You will find the HumanoidLookAt and HumanoidIKLookAt game objects there. The first one uses a generic LookAt() method and the second one uses the Animator.SetLookAtPosition() function. To see the effect, play the game, switch to the Scene View, and move the Target game object around (a...

Action Points - performing an action in a specified spot


Action Points are a common concept used for characters in games that have to perform a certain action in a certain spot. You will find a lot of them in RPG games where NPCs populate towns and perform different actions creating an illusion of a living community. We are going to address a simple case of an Action Point in this recipe.

Getting ready

We are going to use a character with three animations: WalkIdle, and Action. We are also going to use the SetSpeedFromAgent.cs script from the Using Blend Trees to blend walk and run animations recipe in Chapter 5Character Movement. You can also open the provided example Unity project and go to the Chapter 05 Character actions and expressions\Recipe 05 Action points performing an action in a specified spot directory. You will find an Example.scene scene there. Play the game to see the effect—the Humanoid character will approach an Action Point and perform a pick up animation.

How to do it...

Synchronizing an animation with objects in the scene


In this recipe, we will synchronize a character's animation with an object placed in the scene. This is again a common mechanism used in games.

Getting ready

To follow this recipe, you need two animated game objects: a character and an interactive object. In our example, we use a wheel lever that the character can rotate. We use only two animations, WheelStart and WheelLoop, for both the Character and the Wheel objects. The animations have the same number of frames and are synchronized in a 3D package already. You can also open the provided example Unity project and go to the Chapter 05 Character actions and expressions\Recipe 06 Synchronizing an animation with objects in the scene directory. You will find an Example.scene there. Play the game and press the space bar to see the Character and the Wheel play a synchronized animation. You will find all the necessary animations in the Rigs directory.

Character and Wheel objects playing synchronized...

Using IK for interacting with scene objects


Sometimes our characters need to interact with objects in the scene. Most likely we will need to have a solution for picking up items from the ground or other objects. This recipe shows a simple solution for that.

Getting ready

In this recipe, we are using only two animations: Idle and Pickup. The second one makes the character pick something from the ground. You can go to the Chapter 05 Character actions and expressions\Recipe 07 Using IK for interacting with scene objects directory. You will find an Example.scene there. Open it, play the game, and press the space bar to see the character trying to pick up an object from the ground. You can move the object and play the game again. The character will still try to reach the object.

Character trying to pick an object from the ground

How to do it...

To use IK to interac with scene objects, follow these steps:

  1. Import the character to Unity. Make sure to have the Idle and Pickup animations and set the rig...

Animating facial expressions with Blend Shapes


If you want to animate a character talking or changing its facial expression, it is best to use Blend Shapes. This recipe covers creating facial expressions with Blend Shapes exported from a 3D package.

Getting ready

We need several Blend Shapes that store our facial expressions. In this recipe, we will use SmileAngryBlinkLeftBlinkRight, and BrowsDown shapes. Create them in your 3D package. If you are using Blender, Blend Shapes are called Shape Keys. In other softwares, they may be called Morph Targets.

You can also use the provided example Unity project and go to the Chapter 05 Character actions and expressions\Recipe 08 Animating facial expressions with blend shapes directory. You will find an Example.scene there. Open it, play the game, and adjust the sliders on the UI to see the effect. You can find the character with all the Blend Shapes in the Rigs directory.

How to do it...

To use Blend Shapes for facial expressions, follow these steps...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity 5.x Animation Cookbook
Published in: May 2016Publisher: PacktISBN-13: 9781785883910
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 $15.99/month. Cancel anytime

Author (1)

author image
Maciej Szczesnik

Not interested. Too busy with current game project. Source: Linkedin.
Read more about Maciej Szczesnik