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 6. Handling Combat

This chapter explains usage of animations in combat and covers the following topics:

  • Using Sub-State Machines in Animator Controller
  • Using Animation Events to trigger script functions
  • Using transitions from Any State to play hit reactions
  • Using root motion to create a dodge move
  • Checking what Animator state is currently active to disable or enable player actions
  • Using Animation Events to draw a weapon
  • Using Avatar Masks and animator controller layers to walk and aim
  • Using the LookAt() method to aim
  • Using Blend Trees to aim
  • Detecting the hit location on a character

Introduction


Every game is different and every game handles combat mechanics in its own way. Despite that, there is a common set of concepts that are useful in almost every case. In this chapter, we will cover the tools essential for melee and ranged combat.

Using Sub-State Machines in Animator Controller


This recipe shows how to use Sub-State Machines. This concept helps to organize the flow in Animator Controller.

Getting ready

To start, you need to have a character with a few animations. In this example, we are using a character with NormalIdleNormalMoveCombatIdle, and CombatMove animations. These animations will be then grouped in two Sub-State MachinesCombat and Normal.

You can also open the provided example Unity project and go to the Chapter 06 Handling combat\Recipe 01 Using sub state machines in animator controller directory. In the Rigs directory you, will find all the necessary animations. In the Controllers directory, you can find the finished controller with two Sub-State Machines. You can find the Humanoid character in the Example.unity scene. If you press the space bar, our character will switch from Normal to Combat Sub-State Machine.

How to do it...

To create a Sub-State Machine in a controller, follow these steps:

  1. Create and...

Using Animation Events to trigger script functions


We used Animation Events sporadically in earlier recipes because they are so handy, and it was simply difficult to until to this moment. Most present games use Animation Events very extensively, especially for handling combat. Creating believable melee encounters would be a lot harder without this handy tool.

Getting ready

Before we start, we need to have a character with an attack animation. In the example files, we named the animation HumanAttack. We also need an enemy (we use a Spider character in this example) with Idle and Death animations. You can also download the provided example Unity project and go to the Chapter 06 Handling combat\Recipe 02 Using animation events to trigger script functions directory. You will find a scene called Example.unity there, with Humanoid and Spider characters. In the Rigs directory, you can find the Humanoid.fbx character with all required animations. When you play the game, the Humanoid character will...

Using transitions from Any State to play hit reactions


Often, there are situations when we would like to transition to an animation from any other one. The most common example is the hit reaction. Our characters can be hit while standing, walking, or even attacking. Creating and managing all those transitions would be cumbersome and difficult. In Unity, we can use a special Any State in the controllers to achieve transitions from any given animation.

Getting ready

We are going to use the characters from the previous recipe and add one animation to the Spider character: HitReaction. You can also use the example project and go to the Chapter 06 Handling combat\Recipe 03 Using transitions from any state to play hit reactions directory. You can find the Example.unity scene there, with two characters: Humanoid and Spider. When you play the game, Spider will react to hits. In the Rigs directory of the previous recipe, you can find the required animations.

How to do it...

To use Any State transitions...

Using root motion to create a dodge move


We can achieve a lot of gameplay actions in combat using just animations. A great example of such an action is the dodge move.

Getting ready

To follow this recipe, we need a character with SpiderIdle and Dodge animations (we will be using our Spider character in this example). The dodge animation should be created with root motion. Please check the example in the following screenshot:

Spider dodge with root motion translation

You can also go to the Chapter 06 Handling combat\Recipe 04 Using root motion to create a dodge move directory. Open the Example.unity scene there. You will find the same Humanoid and Spider characters we had in previous recipes. They have the same animations and scripts as in the Using transitions from Any State to play hit reactions recipe. The Spider has one additional animation: Dodge. If you hit the space bar in Play Mode, the Spider will attempt to dodge the sword swing. You can find all the necessary animations in the Chapter...

Checking what Animator state is currently active to disable or enable player actions


There are situations when we need to block or allow certain actions depending on the currently played animation. This recipe describes how to easily check what animation is currently being played.

Getting ready

We are going to use the same Spider character from the previous recipe. It has SpiderIdleDodgeHitReaction, and Death animations. You can open the provided example project and go to the Chapter 06 Handling combat\Recipe 05 Checking what animator state is currently active to disable or enable player actions directory. There is a Spider character in the Example.unity scene. When you press the H key, it will start a healing action, only when in SpiderIdle state. You can find all the necessary animations in the Chapter 06 Handling combat\Recipe 02 Using animation events to trigger script functions\Rigs directory.

How to do it...

To check which Animator state is currently active, follow these steps:

  1. Import...

Using Animation Events to draw a weapon


Another common case in combat mechanics is drawing and sheathing a weapon. In this recipe we will use Animation Events along with a Draw animation to make our character draw and sheath a sword.

Getting ready

To follow this recipe, we need to have a Warrior character with two animations: Idle and Draw. The first one is a standard, looped Idle animation. The second one is an animation in which our character reaches for their weapon in a sheath attached to the character's belt. We are not using a sheath model here to make the recipe more simple and condensed. You can open the provided example project and go to the Chapter 06 Handling combat\Recipe 06 Using animation events to draw a weapon directory. There is a Warrior character in the Example.unity scene. When you press the space bar, it will draw or sheath a sword. You can find all the necessary animations in the Rigs directory.

How to do it...

To make a character draw or sheath a weapon, follow these steps...

Using Avatar Masks and animator controller layers to walk and aim


This recipe shows how to use Avatar Masks and Layers in the Animator Controller to play animations on certain body parts. In combat, it can be useful for playing aim animations on the upper body and movement animations on the lower body.

Getting ready

In this recipe, we are using WalkForwardWalkLeftWalkRight, and Idle animations, and a looped AimForward animation. The first four animations are covered in the Using root motion to steer a character recipe in Chapter 4Character Movement. The last one is a simple looped aim animation where our character aims a crossbow straight ahead. You can go to the Chapter 07 Special effects\Recipe 07 Using sprite sheets to animate particles directory. You will find an Example.unity scene there. Open it and play the game. You can move the character with the WSAD keys and rotate the camera with the mouse. You can find the AimForward animation in the Rigs directory and all the other required...

Using the LookAt() method to aim


In this recipe, we will use a simple but effective implementation of aiming mechanics.

Getting ready

We are going to use the same assets as in the previous recipe. Additionally, we are going to use the CharacterLookAt.cs created in the Using Quaternion.LookRotation() and Animator.SetLookAtPosition() methods to make characters follow an object with their gaze recipe in Chapter 5Character Actions and Expressions.

You can also use the provided example Unity project and go to the Chapter 06 Handling combat\Recipe 08 Using the LookAt method to aim directory. You will find an Example.unity scene there, with a Humanoid character. Play the game to see the aiming effect.

How to do it...

To use the LookAt() method for aiming, follow these steps:

  1. Import your character to Unity and place it on the Scene.
  2. Follow the previous recipe to make a character walk and aim.
  3. Attach the CharacterLookAt.cs script to the character.
  4. Create a new Sphere object and name it AimTarget.
  5. Assign...

Using Blend Trees to aim


In this recipe, we will use a more sophisticated method of aiming. This method is based on using several aim animations combined into a Blend Tree.

Getting ready

We are going to use the same assets as in the previous recipe, but instead of using the CharacterLookAt.cs script, we will use additional animations: AimForward, AimForwardUp (aim 45 degrees up), AimForwardDown (45 degrees down), AimLeft (45 degrees left), AimLeftUp (45 degrees left and 45 degrees up), Aim LeftDown (45 degrees left and 45 degrees down), AimRight (45 degrees right), AimRightDown (45 degrees right and 45 degrees down), AimRightUp (45 degrees right and 45 degrees up). See the following screenshot for reference:

Nine directional aim animations

You can also use the provided example Unity project and go to the Chapter 06 Handling combat\Recipe 09 Using blend trees to aim directory. You will find an Example.unity scene there, with a Humanoid character. Play the game to see the effect. You can find...

Detecting the hit location on a character


Another animation-related topic in combat is the hit location detection. This recipe shows how to easily detect hits to different body parts.

Getting ready

We are going to use a simple character with just one looped Idle animation in this recipe. You can also use the provided example Unity project and go to the Chapter 06 Handling combat\Recipe 10 Detecting the hit location on a character directory. You will find an Example.unity scene there, with a Humanoid character. Play the game to see the effect.

How to do it...

To be able to detect hits for different body parts, follow these steps:

  1. Import your character with all the necessary animations to Unity. Place it on the scene.
  1. Create a new Layer and name it BodyParts. To do so, go to the Layers menu and choose the Edit Layers option, as shown in the following screenshot:

Adding a new Layer

  1. Create a new script and name it BodyPart.cs. Create only one public string bodyPartName variable in this script. We will...
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