You're reading from Unity 5.x Animation Cookbook
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.
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
To use math for animating a curve, follow these steps:
- Create an empty game object.
- Add a
Line Renderer
component to it. - Create a new material of your liking and assign it to the
Line Renderer
. - Make sure the
Use
World Space
option in theLine renderer
component is not selected (we will use...
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.
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
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.
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...
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.
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...
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.
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...