Animation in Unity

1. Avatars

Unity uses the Avatar system to identify whether a particular animated model in the layout is humanoid, and which parts of the model correspond to legs, arms, head, and torso. Avatar maps animation from one humanoid character to another, allowing retargeting and reverse Indirect Kinetics (IK).

1. Create an Avatar

After importing a character animation model, you can specify the animation type of the character animation model under the Rig option in the Import Settings panel, including three modes: Legacy, Generic and Humanoid, as shown in the figure below.
insert image description here

(1)Legacy

The old animation type, the animation system launched before Unity4.0, the general animation is imported by the Mecanim system, but the exclusive functions of humanoid animation cannot be used.

(2)Generic

General animation type, supports non-humanoid (monster) animation, also supports humanoid animation, but it cannot use the Humanoid animation redirection function. That is, the model can only use the animation made by its own bones, and cannot be used by other models

(3)Humanoid

Use Humanoid (humanoid animation), as shown in the figure above, click the drop-down list on the right side of the Animation Type option, select Humanoid, and then click Apply, the Mecanim animation system will automatically combine the skeleton structure provided by the user with the simple skeleton that comes with the system Match, if the match is successful, the Configure check box under Avatar Definition will be selected, and in the Assets folder, an Avatar sub-resource will be added to the model resource.

Two, Animation view

insert image description here

1. Animation Timeline

The right side of the animation view panel is the currently edited timeline. The keyframes of each animation property are displayed in this timeline. The timeline view has two modes: keyframe list mode (Dopesheet) and timeline mode. Click the corresponding button to start To switch between usage modes.

(1) Keyframe list timeline mode

Multiple key points can be selected by frame selection. This feature lets you select and manipulate multiple keys at once.
insert image description here

(2) Curved time axis mode

Multiple key points can be selected by frame selection. This feature lets you select and manipulate multiple keys at once.
insert image description here
(3) Playback and frame navigationinsert image description here

2. Create a New Animation Clip

It can be established as shown in the figure below:
insert image description here
you can drag the object to record the animation while recording, or modify the properties in the object’s property panel to record the animation, or you can directly create a certain property of the object on the animation timeline, and then Manually add keyframes on the right side of the axis, and enter the corresponding animation values.

3. Supported Animatable Properties

Float、Color、Vector2、Vector3、Vector4、Quaternion、Boolean。

4. Curve editing

Switch the animation view to Curves, click on the key frame to drag it, modify its tilt angle, or click the right button to set more complex properties.insert image description here

5. Add animation events

Add animation events as shown below:
insert image description here

三、Animator Controller

Create, view, and modify Animator Controller assets in the Animator window. The Animator window has two main parts: the main grid layout area and the Layers and Parameters panels on the left. The main section with the dark gray grid is the layout area. Use this area to create, arrange and connect states in the Animator Controller. The left panel toggles between Parameters view and Layers view. The Parameters view allows you to create, view, and edit Animator Controller parameters. These parameters are variables that you define and serve as inputs to the state machine. To add a parameter, click the plus icon and choose a parameter type from the pop-up menu. To delete a parameter, select the parameter in the list and press the Delete key (use fn-Delete on macOS to delete the selected parameter).
insert image description here

1. Animation parameters

You can update the value of the parameter through the animation curve, and then access the parameter through the script to control some specific attribute values. Scripts can also set parameter values ​​that are picked up by Mecanim, for example, scripts can set parameters to control blend trees. The default parameter value can be set in the Parameters interface of the Animator window. The parameter value is divided into four basic types:
Integer - integer
Float - a number with a decimal part
Bool - true or false value (represented by a check box)
Trigger - when transitioned When used, a boolean parameter (represented by a circular button) that is reset by the controller
insert image description here
can be assigned a value from script using a function in the Animator class:

using UnityEngine;
using System.Collections;

public class SimplePlayer : MonoBehaviour {
    
    
    
    Animator animator;
    
    // 使用此函数进行初始化
    void Start () {
    
    
        animator = GetComponent<Animator>();
    }
    
    // 每帧调用一次 Update
    void Update () {
    
    
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        bool fire = Input.GetButtonDown("Fire1");

        animator.SetFloat("Forward",v);
        animator.SetFloat("Strafe",h);
        animator.SetBool("Fire", fire);
    }

    void OnCollisionEnter(Collision col) {
    
    
        if (col.gameObject.CompareTag("Enemy"))
        {
    
    
            animator.SetTrigger("Die");
        }
    }
}

2. State machine

Created by the Create Sub-State Machine, characters often have complex movements that consist of several stages. It makes sense to identify separate phases and use separate states for each phase, rather than having a single state handle the entire action. Allows you to collapse a group of states into a single specified item in a state machine diagram. These collapsed groups of states are called sub-state machines. To create a sub-state machine, right-click on an empty space in the Animator Controller window and select the Select Create Sub-State Machine from the menu. Sub-state machines are represented in the editor as thin hexagons to distinguish them from normal states.
insert image description here

3. Animation layer

Click the gear on the right side of the window to display the settings for that layer.
insert image description here
On each layer, you can specify a mask (the part of the animated model to which animation is applied) and a blend type. Override means information from other layers will be ignored, while Additive means animation will be added on top of previous layers.
New layers can be added by pressing + above the widget.

The Mask property is used to specify the mask used on this layer. For example, if you only want to play a throwing animation for the upper body of a model, but also allow the character to walk, run, or stand still, you can use a mask on the layer to play the throwing animation where the upper body part is defined: Layers
insert image description here
Side A "M" symbol appears in the column to indicate that the layer has a mask applied.

Animation layer synchronization can realize the reuse of the same state machine in different layers. For example, if you want to simulate the "injured" behavior and generate walking/running/jumping animations in the "injured" state instead of animations in the "healthy" state, You can click the Sync checkbox on one of the tiers and select which tiers to sync. The structure of the state machine would then be the same, but the actual animation clips used by the states would be different.

This means that the synchronized layer does not have its own state machine definition at all, but an instance of the synchronized layer's state machine. Any changes made to the layout or structure of a state machine in the sync layer view (for example, adding/removing states or transitions) are made to the sync layer's source. The only unique changes to synced layers are the selected animations used within each state.

The Timing checkbox allows Animator to adjust the timing (determined by weights) required for each animation in the sync layer. If Timing is unchecked, animations on synced layers are adjusted. This adjustment stretches the length of the animation to match that on the original layer. If checked, the animation length will be balanced between the two animations (based on weights). In both cases (checked and unchecked), Animator will adjust the length of the animation. If unchecked, the original layer will be the only master. If checked, a compromise is used.
insert image description here

4. Mute and Solo

Mute disables the transition
solo and only plays that transition
insert image description here
Multiple solo transitions can be set to play only the solo enabled transition. If one transition is Solo enabled, Unity will enable Mute for the other transitions. If both Solo and Mute are enabled, Mute will take priority.

Controller graphs do not always reflect the engine's internal mute state.

5. Target Match

Often times in games the following situation can arise: A character has to move in a certain way such that a hand or foot lands somewhere at a certain time. For example, a character might need to jump over stepping stones or jump and grab a top beam. Imagine a situation where you want to arrange for a character to jump onto a platform, and you already have an animation clip called Jump Up for this situation. First, you need to find the position in the animation clip where the character starts to lift off the ground, notice in this example, this position is 14.1% or 0.141 of the normalized time in the animation clip, you also need to find the position in the animation clip where the character is about to land, In this example, this position is 78.0% or 0.78.

using UnityEngine;
using System;

[RequireComponent(typeof(Animator))] 
public class TargetCtrl : MonoBehaviour {
    
    

    protected Animator animator;    
    
    //场景中的平台对象
    public Transform jumpTarget = null; 
    void Start () {
    
    
        animator = GetComponent<Animator>();
    }
    
    void Update () {
    
    
        if(animator) {
    
    
            if(Input.GetButton("Fire1"))         
                animator.MatchTarget(jumpTarget.position, jumpTarget.rotation, 
                AvatarTarget.LeftFoot, new MatchTargetWeightMask(Vector3.one, 1f), 
                0.141f, 0.78f);
        }       
    }
}

6. Inverse Kinetics

Most animations are generated by rotating joint angles in a skeleton to predetermined values. The position of the child joint changes according to the rotation of the parent joint, so the end point of the joint chain can be determined from the angles and relative positions of the joints contained in the parent joint. This method of building a skeleton is called forward kinetics.

However, it is often useful to look at the task of building joints from the opposite perspective: having chosen a location in space, work backwards to find an efficient way to position the joints such that the end point falls at that location. This can be useful if you want your character to touch an object at a user-selected location, or to plant your character's feet firmly into an uneven surface. This method is called inverse kinematics (IK) and can be used in Mecanim for any humanoid Avatar skeleton that has been properly configured.
insert image description here
To set IK for a character, you typically place objects around the scene that interact with the character, and then set the IK via script (in particular, Animator functions such as SetIKPositionWeight, SetIKRotationWeight, SetIKPosition, SetIKRotation, SetLookAtPosition, bodyPosition, bodyRotation)

In the image above, we show a character grabbing a cylindrical object. How do we make it happen?

We start by having a valid Avatar character.

The next step is to create an Animator Controller that contains at least one animation for this character. Then, in the Layers panel of the Animator window, click the gear settings icon for the layer and check the IK Pass checkbox in the popup.
insert image description here
Sets the IK Pass checkbox for the default layer.

Make sure the Animator Controller is assigned to the character's Animator Component:
insert image description here
Next, attach a script to it that actually handles the IK, call it IKControl. This script sets the IK target for the character's right hand, and sets the character's view position so that it looks at the object it's holding:

using UnityEngine;
using System;
using System.Collections;

[RequireComponent(typeof(Animator))] 

public class IKControl : MonoBehaviour {
    
    
    
    protected Animator animator;
    
    public bool ikActive = false;
    public Transform rightHandObj = null;
    public Transform lookObj = null;

    void Start () 
    {
    
    
        animator = GetComponent<Animator>();
    }
    
    // 用于计算 IK 的回调
    void OnAnimatorIK()
    {
    
    
        if(animator) {
    
    
            
            // 如果 IK 处于活动状态,请将位置和旋转直接设置为目标。
            if(ikActive) {
    
    

                // 设置观察目标位置(如果已分配)
                if(lookObj != null) {
    
    
                    animator.SetLookAtWeight(1);
                    animator.SetLookAtPosition(lookObj.position);
                }    

                // 设置右手目标位置和旋转(如果已分配)
                if(rightHandObj != null) {
    
    
                    animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
                    animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);  
                    animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
                    animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);
                }        
                
            }
            
            // 如果 IK 未处于活动状态,请将手和头部的位置和旋转设置回原始位置
            else {
    
              
                animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);
                animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0); 
                animator.SetLookAtWeight(0);
            }
        }
    }    
}

Since we're not going to have the character's hand reach into the inner center of the object (the pivot point of the cylinder), place an empty sub-object (named "Cylinder Grab Handle" in this case), making sure The hand should rest on the cylinder and rotate it accordingly. Then, the hand targets this sub-object.
insert image description here
An empty child GameObject acts as the IK target, so the hand will be placed correctly on the visible cylinder object

This Grab Handle game object should then be assigned as the "Right Hand Obj" property of the IKControl script

In this example, we set the look target to be the cylinder itself, so even though the handle is near the bottom, the character looks directly at the center of the object.
insert image description here

4. Hybrid tree

1. Create a blend tree

Put the mouse cursor on the blank area of ​​the grid and right-click Create State->From New Blend Tree:
insert image description here
insert image description here
When using animation clips and input parameters to set up a blend tree, the Inspector window will graphically display how the animation is combined as the parameter value changes (drag the slider , the arrow in the root of the tree changes its coloring to show the animation clip it controls).
insert image description here

2. Hybrid type

(1) 1D mixing:

The first option in the Blend node's Inspector is Blend Type. This drop-down menu is used to select one of the different blend types; these types can blend based on one or two parameters. 1D blending blends sub-motions according to a single parameter.
insert image description here

(2) 2D blending:

2D Simple Directional: Best when motion represents different directions (e.g. "walk forward", "backward", "walk left" and "walk right" or "aim up", "aim down", "left Aim" and "Aim Right"). A single movement at position (0, 0), such as Idle or Aim, can be included as desired. In the Simple Directional type, there should _NOT_ be more than one motion in the same direction, eg "walk forward" and "run forward".

2D Freeform Directional: This blend type is also used when the motion represents different directions, but you can have multiple motions in the same direction, such as "walk forward" and "run forward". In the Freeform Directional type, the motion set should always include a single motion at position (0, 0), eg "idle".

2D Freeform Cartesian: Best used when motion does not represent different directions. With Freeform Cartesian, X and Y parameters can represent different concepts such as angular velocity and linear velocity. An example is movements such as "walk forward without turning", "run forward without turning", "walk forward and turn right", "run forward and turn right".
insert image description here

(3) Direct mixing:

Use a direct blend tree to map Animator parameters to the weights of BlendTree children. This is useful if you want precise control over the various animations being blended rather than blending them indirectly with a parameter or two (as is the case with 1D and 2D blend trees).
insert image description here

3.Animator Override Controller

An Animator Override Controller is a resource that allows you to extend an existing Animator Controller, replacing the specific animation used but retaining its original structure, parameters, and logic.

Therefore, you can create multiple variants of the same basic state machine, but each variant uses a different set of animations. For example, your game may have various NPC-like characters living in the world, but each type (goblin, demon, pixie, etc.) has its own unique animations for walking, idling, sitting, etc.

By creating a "base" Animator Controller that contains all NPC type character logic, an override can be created for each character type and put into its own animation file.
insert image description here

5. Performance and optimization

1. Animation system

(1) Controller

Animators that don't have controllers set don't take time to perform processing.

(2) Simple animation

Playing a single animation clip without blending will make Unity slower than older animation systems. The old system was very straightforward, sampling the curve and writing it directly into the transform. Unity's current animation system has temporary buffers for blending and makes additional copies of sample curves and other data. The current system layout is optimized for animation blending and more complex setups.

(3) Zoom curve

Animating scale curves is more expensive than animating move and rotate curves. To improve performance, avoid scaling animations.

NOTE: This does not work for constant curves (curves with the same animation clip length value). Constant curves are optimized and cost less than normal curves. When the value of the constant curve is the same as the default scene value, the constant curve is not written to the scene every frame.

(4th floor

Unity estimates animations most of the time and keeps the overhead of animation layers and animation state machines to a minimum. The cost of adding another layer to the Animator (whether synced or not) depends on the animation played by the layer and the blend tree. When a layer's weight is zero, Unity skips layer updates.

(5) Humanoid animation type and general animation type

The following tips can help you choose a specific type:

When importing humanoid animation, if you don't need IK (Inverse Kinetics) targets or finger animation, use an Avatar mask (class-AvatarMask) to remove them.
Using a root motion is more costly than not using a root motion when using generic types. If the animation is not using root motion, make sure no root bone is assigned.

(6) Scene level optimization

Many optimizations are possible, some useful hints are as follows:

Use a hash instead of a string to query the Animator.
Implement a small AI layer to control the Animator. You can have it provide simple callbacks for OnStateChange, OnTransitionBegin, and other events.
Use state markers to easily match AI state machines to Unity state machines.
Use other curves to simulate events.
Use additional curves to mark animations; for example, with target matching.

2. Runtime optimization

Always optimize animations by setting the Animator's Culling Mode to Based on Renderers, and disable the Skinned Mesh Renderer's Update When Offscreen property. This keeps Unity from having to update animations when the character is not visible.

References:
Unity Official Manual

Guess you like

Origin blog.csdn.net/qq_45548042/article/details/120973774