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 4. Character Movement

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

  • Using Blend Trees to blend walk and run animations
  • Using root motion to drive Rigid Body characters' movement with animations
  • Using root motion to steer a character
  • Using animations for better looking transition
  • Using root motion for a 180 degrees turn
  • Making a character jump with 3-phase animation
  • Using root motion to drive Nav Mesh Agents' movement with animations
  • Using triggers to grab an edge while jumping
  • Changing the character's collision while crouching
  • Adding animation to off-mesh links
  • Using root motion for climbing
  • Using root motion to create flying characters

Introduction


Character movement is an essential part of almost every game. In this chapter, we will explore different possibilities, from simple walk and run blending to complex animation-driven movement.

Using Blend Trees to blend walk and run animations


In this first recipe, we will get familiar with Blend Trees (a new type of states in the Animator Controller). Blend Trees allow to smoothly blend multiple animations together. A common example for using them is blending walk and run cycles.

Getting ready

Before we start, you should have a character with at least three looped animations: idle, walk (in place), and run (also in place). You can download the provided example; open the project in Unity and go to the Chapter 04 Character movement\Recipe 01 Using blend trees to blend walk and run animations folder. You will find a scene called Example.unity there, with an animated humanoid character. If you play the game, you can click on the ground to move the character, and if you press Shift while moving, the character will blend to run animation smoothly. In the Rigs directory, you can find the Humanoid.fbx asset with the required animations.

How to do it...

To create a smooth blend between walk...

Using root motion to drive Rigid Body characters' movement with animations


This recipe describes a very important concept called root motion. It allows the extraction of translation and rotation data from an animation and applies it to our character in the game.

Getting ready

To use root motion, first you need to create a character with a walk or run animation, that has translation in it; see the following screenshot:

Frames of a run animation using root motion. The character is animated with root node translation

If you are using a Humanoid character, the hip bone is used as the root node, the one describing root motion. So hip translation will describe the translation of the character in game (you can still have motion in the Y axis as we can then adjust it in the Import settings). Its rotation will describe character rotation in the game. If you are using a Generic character, you need to choose the Root Node manually in the Import settings. Select the model, go to Inspector, click on the...

Using root motion to steer a character


In this recipe, we will use animations to move and steer our character.

Getting ready

To use root motion for steering, you need to prepare a character with at least four animations: Idle, WalkForward, WalkLeft, and WalkRight. WalkRight and WalkLeft should make the character walk in circles (clockwise and counterclockwise). You don't need to create a full circle, just make sure that the start and end poses of the animation look similar. As always, all the animations should have contact points of the feet in the same normalized time. Make sure not to switch the left and right foot. If you start the WalkForward animation with the left foot forward, both WalkLeft and WalkRight animations should also start with the left foot forward, as shown in the following screenshot:

Steering animations using root motion

You can also use the example project; go to the Chapter 04 Character movement\Recipe 03 Using root motion to steer a character directory. You can find the...

Using animations for better looking transitions


You've probably already noticed that sometimes when our character stops walking, it blends to the Idle animation in a strange way (with sliding feet). That is mostly visible when legs in the Walkanimation are in a pose that looks like a mirrored Idle pose. We are going to fix this problem in this recipe.

Getting ready

You should use the same character as in the previous recipe and prepare one additional animation that will be a transition from a mirrored Idle pose to a normal Idle pose. You can also go to the Chapter 04 Character movement\Recipe 04 Using animations for better looking transitions directory and open the Example.unity scene. You will find our Humanoid character working the same way as in the previous recipe, but with a ToIdle animation added. You can also find the rig with all required animations in the Rigs directory.

How to do it...

To use additional animations for a better-looking transition, follow these steps:

  1. First, follow all...

Using root motion for a 180 degrees turn


In the previous recipe, we've added a stop animation for a better transition to the idle state. We can add even more animations to our movement to make it more accurate and responsive. An example of such animation is a 180-degree turn.

Getting ready

Use the same character as in the previous recipe. Add a 180-degree turn animation, starting with the Idle pose and ending with the Idle pose rotated by 180 degrees in the vertical axis. Name the animation 180Turn for clarity. You can open the provided example Unity project and go to the Chapter 04 Character movement\Recipe 05 Using root motion for a 180 degrees turn directory. You will find an Example.unity scene there. Play the game and try to walk in the direction opposite to the one the character is facing (press the down arrow). You can find the Humanoid.fbx asset in the Rigs directory with all the required animations.

How to do it...

To make a 180-degree turn, follow these steps:

  1. Create four transitions...

Making a character jump with 3-phase animation


In this recipe, we will make our character jump. Jumping is best done with physics, so we will use this approach.

Getting ready

Before we start, you should add three more animations to your character: a short Jump animation starting on the ground in a pose similar to Idle; an InAir animation, a looped animation of the character being in the air; and a Land animation starting when the character touches the ground with his feet and ending with the Idle pose. All those animations should be done "in place" without root node translation. You can open the provided example Unity project and go to the Chapter 04 Character movement\Recipe 06 Making a character jump with 3-phase animation directory. You will find an Example.unity scene there. Play the game and press the space bar to see the character jump. You will find all the animations needed in the Rigs directory.

How to do it...

To make the character jump, follow these steps:

  1. Import the character and...

Using root motion to drive a NavMesh Agents' movement with animations


In this recipe, we will use root motion to move and steer a character with a NavMesh Agent component. This can be used to get rid of foot sliding in this type of character.

Getting ready

We are going to use the same character as in the previous recipe with all its animations. You should have a character with at least the Idle, WalkLeft, WalkForward, and WalkRight animations ready and set up in the Animator Controller the same way as in the Using root motion to steer a character recipe. You can also go to the Chapter 04 Character movement\Recipe 07 Using root motion to drive Navmesh Agents movement with animations directory. You will find an Example.unity scene there. Open it, play the game, and click on the ground to make the characters move using NavMesh Agents and root motion.

How to do it...

To use root motion for moving and steering a character with Nav Mesh Agent component, follow these steps:

  1. Import the character with...

Using triggers to grab an edge while jumping


Grabbing an edge while jumping is a common feature, especially in platform games. It is easily done in Unity with a small amount of scripting. This recipe covers a simple edge grab functionality.

Getting ready

You need three new animations for our character: EdgeGrab, EdgeGrabLoop, and EdgeGrabClimb. The first one is a transition from the InAir animation to the EdgeGrabLoop animation. It should have minimum movement in the root node if possible. The second one is a looped animation of hanging on the cliff's edge. The last one is an animation that uses root motion to climb the cliff's edge and ends with an Idle pose. See the following screenshot for reference:

Frames of the EdgeGrabClimb animation using root motion to climb over the cliff's edge

EdgeGrab and EdgeGrabLoop animations should have all Bake Into Pose options selected in Import Settings. EdgeGrabLoop should also have the Loop Time option selected. You can also use the provided example Unity...

Changing the character's collision while crouching


In this recipe, we will learn how to change our character's collision while crouching. This way our character will be able to enter low areas (such as vent shafts).

Getting ready

We are going to use the same character as in the Using root motion to steer a character recipe with all its animations (Idle, WalkLeft, WalkForward, and WalkRight). Additionally, you should prepare those four animations in a crouch state (IdleCrouch, WalkLeftCrouch, WalkForwardCrouch, and WalkRightCrouch). We are going to use the RootMotionSteering.cs script from the preceding mentioned recipe. You also need an obstacle in your scene. It should have a passage underneath. The passage's ceiling should be low enough so that the character cannot go through it without crouching. We don't need any other scripts to start. You can also go to the Chapter 04 Character movement\Recipe 09 Changing the characters collision while crouching directory. You will find an Example.unity...

Using root motion for climbing


In this recipe, we will use root motion animations to climb a ladder.

Getting ready

We are going to use the same character as in the Using root motion to steer a character recipe with the Idle, WalkLeft, WalkForward, and WalkRight animations. You should prepare four additional animations: ToClimb (it should start with idle and end with ClimbIdle and should be done "in place" without hip movement), ClimbIdle (a looped animation where our character is idle on the ladder), ClimbUp (a looped animation, where our character moves one ladder step up; it starts and ends with ClimbIdle pose), and ClimbEnd (an animation similar to the EdgeGrabClimb from the Using triggers to grab an edge while jumping recipe). We are going to use the RootMotionSteering.cs script from the Using root motion to steer a character recipe to make the character move. You also need a ladder model with which your ClimbUp animation is synced. You can also go to the Chapter 04 Character movement...

Using root motion to create flying characters


We've seen a lot of "hovering" monsters in games in the past, but creatures flying in full 3D space are still a quite fresh and tasty feature. Creating a behavior of such a monster with code is challenging, but we can use animations to make it both easy and great looking.

Getting ready

To create a flying creature in this recipe, we need one animation without root motion, Idle (hovering in place), and nine animations with root motion: FlyingForward (flying straight forward), FlyingForwardUp (flying forward and increasing altitude at the same time), FlyingForwardDown (flying forward and decreasing altitude at the same time), FlyingLeft (flying left in circles-the same way as the WalkLeft animation in previous recipes), FlyingRight (flying right in circles), FlyingLeftUp (flying left in circles with increasing altitude), FlyingLeftDown (flying left in circles with decreasing altitude), FlyingRightUp (flying right in circles with increasing altitude...

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