https://github.com/PacktPublishing/Unity-5.x-Animation-Cookbook

https://assetstore.unity.com/packages/tools/animation/final-ik-14290

 

项目Unity版本 5.3.4f1 

 

 

Chapter 1: Working with Animations (已看)

Chapter 2: Working with the Animation View (已看)

Chapter 3: 2D and User Interface Animation (已看)

Chapter 4: Character Movement (已看)

Chapter 5: Character Actions and Expressions (已看)

Chapter 6: Handling Combat

Chapter 7: Special Effects

Chapter 8: Animating Cutscenes

Chapter 9: Physics and Animations

Chapter 10: Miscellaneous

 

Chapter 1: Working with Animations

This chapter explains the essentials of working with animations in Unity and covers the following topics:

  • Importing skeletal animations
  • Configuring generic and humanoid rigs
  • Creating and assigning an Animator Controller
  • Creating animation transitions in Animator Controller
  • Using parameters to control the animation flow
  • Using animations from multiple assets
  • Looping mirroring and offsetting animations
  • Adjusting the playback speed of animations
  • Using override Animator Controllers to animate different types of characters
  • Importing object animation from a 3D package

  Introduction

animate everything

  Importing skeletal animations

skeleton (called rig in Unity)

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

Unity uses four different rig configurations:

  • Generic rig: This one is used for quadrupeds, spiders, and other non-humanoid chracters. It uses Unity's Mecanim system for controlling animation flow. It can use root motion if you provide a Root node - a bone responsible for character movement
  • Humanoid rig: This one is used for humanoid characters only. It also uses Mecanim system for controlling animation. If you are using Humanoid rigs, you have more options for importing animations. The biggest advantage is automatic animation retargeting - you can use the same animation clip on various types of humanoid characters (for example, a dwarf and atroll)
  • Legacy: This option is used mostly for backward compatibility with previous versions of Unity. It is similar to the Generic rig but uses scripts instead of Mecanim for controlling animation flow. I recommend using Generic rig instead
  • None: This last option turns off the rig and disable animation import

  Configuring generic and humanoid rigs

If you are creating your characters from scratch, the bones hierarchy of your humanoid rig is important for Unity to recognize the rig as a humanoid. It should follow this pattern:

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

Generic

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

 

Humanoid

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

 

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)(bone mapping)

 

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

Make sure you are in the Mapping section (1). All recognized and assigned bones are shown as green body parts on the displayed dummy character. If a required bone is missing or is not assigned, it will show up as red. You can navigate between the Body, Head, Left Hand, and Right Hand sections by clicking on a corresponding button (2). All bones are displayed in a list (3) for each section. Required bones are marked with circle icons and optional bones are marked with dotted circle icons.

Body

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

Head

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

Left Hand

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

Right Hand

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

Humanoid rig uses more advanced Mecanim features than the Generic rig.

  • Automatic retargeting: This is one of the most important differences between those two rigs. The Humanoid rig uses automatic retargeting, which means thatyou can have the same animations on different humanoid characters. Your characters can share a group of animations (basic movement or some common actions). It gives you the possibility to buy your animation assets from the Asset Store and easily use them on your characters. Generic rigs don't support this feature. You have to prepare animations for your specific rig or retarget them in a 3D package.
  • Inverse kinematics: This feature lets you control your characters' feet and hands position with scripting. It is useful for making your characters stand on uneven ground or grab an object in the scene. It is a built-in feature for Humanoid rigs in Unity. Generic rigs have to use custom-made solutions.
  • Advanced animation settings: Humanoid rigs have more settings for animation import, such as the mirror option. We will discuss them in depth in the Looping mirroring and offsetting animations recipe.
  • Look at: Unity has a built-in solution for humanoid characters looking at something. You have to write custom systems for generic characters.
  • Additional bones: A lot of people think that they cannot use additional bones with Humanoid rigs. It is not true. If your rig has an animated weapon slot for instance, you can still use it with the Humanoid rig. All you need to do is to find the Mask section in your animation import settings and enable the additional bone for each animation it is used in (you need to use the Transform foldout to find your additional bone).

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

You can preview the range of movement in the Muscle Group Preview section by adjusting the sliders (1). You can preview the range of movement per muscle in the Per-Muscle Settings section. You can also adjust the range here by unfolding a given muscle foldout and using the slider (2). In the Additional Settings section, you can adjust more options of your avatar. These are mainly responsible for the flexibility of your rig. You can set how much a bone can be stretched during animation for instance.

The Translation DoF option enables animating bones transition in your Humanoid rig. It is turned off by default, meaning that only rotation of the bones is used in animation.

In the Rig tab in the model import settings, you can find some additional options

  • Avatar Definition: This option is responsible for creating a new avatar or copying the avatar from another model. The second option is useful for storing animations in multiple files. It will be discussed further in the Using animations from multiple assets recipe.
  • Optimize Game Objects: This option lets you hide all bones from the Hierarchy view. The number of game objects in the game has an impact on the performance. You can still choose a number of bones that will be displayed in the Hierarchy. It is useful for having exposing weapon slots and similar gameplay-related bones while hiding others.
  • Root node: This option is visible only for Generic rigs. It lets you choose the bone responsible for root motion calculation

  Creating and assigning an Animator Controller

Animator Controllers are state machines (graphs) responsible for controlling the flow of animations of any animated object in the game. The same Animator Controller asset can be used by multiple objects or characters. Unity will create an independent runtime copy of the asset for each animated object it is assigned to.

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

  • Controller: This is the field we have to attach the Animator Controller asset to. It determines which animation graph the Animator component will use.
  • Avatar: In Unity, Avatars are rig definitions. For instance, if we have multiple files containing animations with the same Generic rig, we should use the same Avatar for all of them. You can find more information about it in the Using animations from multiple assets recipe.
  • Apply Root Motion: With this checkbox, we can turn the root motion on and off. It can be useful when we have animations with root motion but don't want to use the root motion definition for a given character.
  • Update Mode: This parameter tells Unity in which update the animations should be evaluated. The Normal option makes the animations synchronized with the normal Update() call, the Animate Physics option synchronizes animations with the physics FixedUpdate() call, and the Unscaled Time option synchronizes the animation with the normal Update() call, but disables animation time scaling (the animation is played with 100 percent speed regardless of the Time.timeScale variable value).
  • Culling Mode: This parameter tells Unity when to turn off the animation playback on a given Animator. The Always Animate option makes the Animator always play animations (event when off-screen), the Cull Update Transforms option culls Retarget and IK Transforms when the Animator is not visible on screen, and the Cull Completely option disables the animation completely when the Animator is not visible on screen.

  Creating animation transitions in Animator Controller

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

State transition define how we can travel through the graph of animations

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

  • Has Exit Time: If set to true, this enables the transition only after a given percentage of the animation has been already played. If disabled, the transition will take place instantly after its conditions are met. If you want to create a sequence of animations, set it to true.
  • Exit Time: This is the percentage (0-1 range) of the animation length after which the Has Exit Time condition is met. If you set it to 0.5, for instance, the state will transition after 50 percent of the animation was played.
  • Fixed Duration: This property is combined with the next one, Transition Duration (s). If it's on, the Transition Duration (s) is in seconds, and if it's off, the Transition Duration (s) is in percentage of the animation time.
  • Transition Duration (s): the time of the state transition. This is how long the animations will transition one into another. Animations are being blended together during the transition. Longer transitions are smoother, shorter, are more sharp. A good default value is around 0.25.
  • Transition Offset: This offsets the target animation in time. The value is in percentage of the animation. Setting it to 0.3 means that the target animation will start from 30 percent of its length instead of the beginning.
  • Interruption Source: This setting tells Unity whether the transition can be interrupted by other transitions. You can set it to:

    None: the transition cannot be interrupted by any other transition.

    Current State: The transition can be interrupted by transitions from the state we are trying to transition from.

    Next State: The transition can be interrupted by transition from the state we are trying to transition to.

    Current State then Next State: The transition can be interrupted by the transitions of the state we are trying to transition from or by the transitions of the state we are trying to transition to. The transitions from the state we are trying to transition from take the priority.

    Next State then Current State: The transition can be interrupted by the transitions of the state we are trying to transition from or by the transitions of the state we are trying to transition to. The transitions from the state we are trying to transition to take the priority.

    Ordered Interruption: If set to false, this lets the transition be interrupted by other transitions independently of their order. If set to true,the order of the transitions matters.

When you create an Animator Controller, you can see three more nodes apart from your animation states

  • Entry and Exit: This node is used when you transition between state machines (Animator Controllers or substate machines). 
  • Any state: This node can be used as a helper to make a transition from any state to a given state. It is used when you have an animation that can be played anytime, for instance, a hit animation.

  Using parameters to control the animation flow

Unity 5.x Animation Cookbook (Maciej Szcześnik 著)

using UnityEngine;
using System.Collections;

public class Wave : MonoBehaviour {
    //The anim variable is used to store the reference
    //to the Animator component of the character.
    private Animator anim;

    void Start () {
        //We get the component and assign it to
        //the anim variable when the game starts
        anim = GetComponent<Animator>();
    }

    void Update () {
        //We check if player pressed the spacebar
        if (Input.GetKeyDown(KeyCode.Space))
        {
            /*We cal the SetTrigger() function on the Animator component stored in the anim variable. The function requires one
parameter - the name of the trigger parameter set in our Animator Controller ("Wave" in our example). Make sure to match
it with the name of the parameter you've created in your Animator Controller*/
            anim.SetTrigger("Wave");
        }
    }
}
View Code

相关文章: