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 10. Miscellaneous

This chapter contains a number of extra recipes:

  • Using math to animate an object
  • Using the Lerp() function to animate an object
  • Using the Rotate() function to animate an object
  • Preparing motion capture files for humanoid characters
  • Adding behaviors to Mecanim states

Introduction


This chapter contains additional recipes with useful tricks and solutions.

Using math to animate an object


We can use mathematical formulas to create interesting-looking animations. We are going to use this concept to make an endless curve animation with the Mathf.Sin() and Mathf.Cos() functions.

 

Getting ready

We don't need anything special for this recipe. We are going to create it from scratch in Unity. You may also download the provided example project. Open the Chapter 10 Miscellaneous\Recipe 01 Using math to animate an object directory and load the Example.unity scene there. If you run the game, you will see the finished effect—a curve animated with the Mathf.Sin() and Mathf.Cos() functions:

Final effect—curve animated with the Mathf.Sin() and Mathf.Cos() functions

How to do it...

To use math for animating a curve, follow these steps:

  1. Create an empty game object.
  2. Add a Line Renderer component to it.
  3. Create a new material of your liking and assign it to the Line Renderer.
  4. Make sure the Use World Space option in the Line renderer component is not selected (we will use...

Using the Lerp() function to animate an object


We were using the Lerp() method previously, but it is important to know that we can use it in two different ways. This recipe covers both of these uses.

Getting ready

We are going to create this recipe from scratch and we don't need any special assets. You can download the example project and go to the Chapter 10 Miscellaneous\Recipe 02 Using the Lerp function to animate an object directory. Open the Example.unity scene there and play the game. You will see two objects: RedDot and BlueDot (both are children of the Canvas object). The first one uses the standard Lerp() method and interpolates its position from a minimum to a maximum value in time. The second one uses the Lerp() method in a different way and continuously follows the mouse pointer.

RedDot interpolates its position between min and max and BlueDot follows the cursor

How to do it...

To use the Lerp()function for animating objects, follow these steps:

  1. First we need to create some objects...

Using the Rotate() function to animate an object


In this recipe, we will use the Transform.Rotate() function to create an infinite looping animation of a windmill. We could make the same with the Animation View, but sometimes writing a simple script means much less effort than creating a key frame animation.

Getting ready

We need a few models we can rotate in this recipe. As an example, we will use a windmill. It is composed of three game objects: WindmillBase, which is the root of the whole windmill, Windmill which is parented to the WindmillBase, and WindmillWings which is parented to the Windmill. The Windmill game object itself can rotate in the Y axis. The WindmillWings game object can rotate in its local Z axis.

Windmill hierarchy

You can download the example project and go to the Chapter 10 Miscellaneous\Recipe 03 Using the Rotate function to animate an object directory. Open the Example.unity scene there and play the game. You will see the windmill rotating around the Y axis and its...

Preparing motion capture files for humanoid characters


Using motion capture files is not always easy. In most cases, we can use them directly (after exporting them to an FBX file) for characters using the Generic rig type. But we almost always want to use our motion capture files for humanoid characters. The problem occurs when our motion capture rig is not a standard Unity Humanoid rig (and again, it is almost always the case). We can expect all kinds of errors—bones rotating with an offset, deformations in the mesh, and so on.

This recipe shows how to retarget motion capture files onto a rig suitable for Unity humanoid animations. We are going to use Blender 3D.

 

Getting ready

To follow this recipe, you will need a humanoid character ready to be rigged to a proper Unity Humanoid rig. We also need a motion capture animation. You can find a huge free-to-use library of motion capture data on the Carnegie Mellon University's website at http://mocap.cs.cmu.edu/. We are going to use the 01_01.bvh...

Adding behaviors to Mecanim states


In this last recipe, we will cover behaviors. These are scripts that can be attached to Mecanim states. You can use them to turn standard Animator Controllers into logic graphs such as AI trees, quests, and so on.

Getting ready

We are going to create this recipe from scratch and we don't need any special assets. You can download the example project and go to the Chapter 10 Miscellaneous\Recipe 05 Adding behaviors to Mecanim states directory. Open the Example.unity scene there and play the game. If you play the game, you can press the space bar to change the states in the Animator Controller attached to the Controller game object. The cycle starts in the NoBehaviors state—this is simply an empty state with no additional behavior. If we press the space bar, we transition to Light1Random. This state changes the color of the Light1 light in the scene. If we press the space bar again, the state will change to WaitAndSwitch. This state waits for a given time and...

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