Unity's study of animator animation system

Source: Internet
Author: User
Tags function definition

Unity's new animation system, called Mecanim, uses animator to replace the old system animation, according to the conventions of the Unity document: The knowledge points are divided into 2 parts: Unity Manual and Unity script, readers can look at the document while viewing the documents, It's best to test it.

Before the beginning of the article, a few basic knowledge is first spoken:

1. One of the basic steps in creating an animation is to set up a unity3d-understandable mapping of the actual skeleton in the skeleton, and in Mecanim's terminology, this mapping is called Avatar, which is the skeleton-to-skeleton mapping of Avatar.

(Pictures from the net)

Avatar is mainly used for human skeleton model, which can realize the retargeting between roles. A non-class human model can be thought of as skeleton.

2. Basic steps for building a model:

Modelling->rigging->skinning (modeling, building skeleton, skinning)

1.modelling Modeling:

1.Observe a sensible topology (following a reasonable topological structure), a reasonable standard is that animation-driven mesh deformation is beautiful;

2. Note the scale of the grid. It's best to do the import test of each modeling software model to set the correct scaling ratio (different modeling software import scale is not the same)

3. Placing the character allows the foot of the character to stand at the origin of the coordinates or the "anchor point" of the model. A character usually walks vertically on the ground, and if the character's anchor point (that is, his transformation center) is easier to control on the ground.

4. If you are a human-like model, try to use T-Pose modeling (unity provides many features and optimizations for the human-like model)

5. Organize your models and get rid of the rubbish. If possible, cover holes, weld vertices, and remove hidden faces, which can help with skinning, especially the automatic skin process.

2.Rigging skeleton: Create the joints on the skeleton to control your model for movement.

Non-class human model words, can be thought of no skeleton, only bone, bone direct control animation, class human model is skeleton control animation. The model in step 1 already has legs, hands, head, weapons and other bones, as well as a hit skeleton, which can be used to control the model or to hang extra objects.

3.Skinning skinning: Attach a mesh to the skeleton.

1. Bind vertices in the grid to bones, including hard bindings (one vertex specifies a bone, not one by one corresponds, possibly multiple vertices specify a bone) and soft binding (one vertex specifies multiple bones, each with a certain weight)
2. The actual operation of the skin: the first automatic skin, followed by a test animation to see the skin effect and slowly change the effect of the change.
3. Bind up to 4 bones per vertex, which is the upper limit of u3d

3. After the animation file was imported into unity, we processed and set it on the Inspector panel: Animation Importing settings

  

Divided into 3 tabs:

Model: Here the parameters are basically determined by the art, the other uses the default is good, here only 4 parameters:

1.scale factor model Scale, different modeling software import ratio, Unity's physical unit is 1m, this according to different modeling software settings

2.readable/writeable and Uitexture, if you open unity you have to copy a copy to the memory, try not to open.
3.Generate colliders is typically used to generate the mesh collider of the scene, so don't open it anywhere else.
4.normals&tangents is the processing of vertices and tangents when importing, such as whether to import the vertices of the model, etc., can limit the model precision and affect subsequent rendering, such as vfshader need to use the model of normal.

      

Rig: Here Animation type includes (Generic/humanoid/legacy/none)

Generic for non-class human models; humanoid for human-like models

Avatar Definition: Can use the existing Avatar, can also create from the this model, generally, the model grid file selected Create, this time, the Avatar sub-asset is added to the model asset below, That is, avatar is generated by unity based on the grid file, the humanoid type, will automatically match the skeleton to the skeleton, the non-conformance will be error, and the animation file using copy, using the existing resources.

    

Animations: Set a animation clip animation segment

The settings in the previous section do not speak here, we see the start and end parameters in the clip below, which means to intercept a paragraph from an FBX animation for clip use, multiple clips share an animated resource, so you can get all the actions of a model into an animated file, Each action goes inside to take a paragraph to be able, also can a model each action has an animation file, manages separately.

    

In the middle of the parameter is more irritable, skip, first mention the animations the bottom of a few interesting parameters: Curves,events,mask, these 2 simple parameters can bring a lot of interesting features, will be described later.

    

After finishing the above basics, let's take a look at some interesting applications:

1.AnimationEvent

Unity Manual section:

Under the Animations tab, you can add a frame event to clip, which triggers an event when it is broadcast to a frame:

1. Add event to some frames of clip, this function is the name of the event, and the other is the parameter

2. Define a script to accept this event, such as this diagram needs to define a scripting, and the script defines the void Ani (XXX) {} function

3. Parameter processing: According to the script function definition format to pass parameters, such as void Ani (int a), then int that argument, Ani (object A) is the argument of the object, ANI (float A, string b) The 2 parameters of float and string are passed, and ANI (Animationnevent a) passes the entire event (including information about all parameters and the clip corresponding to the current event)

PS: Animator object with clip with event must hang up the script that defines the function of the event name, otherwise it will error

    

Unity Script section: You can view the class animationevent, primarily to get some information about the clip in which the event is located: Stateinfo,clipinfo associated with this event, And the event itself: the function name of the event call functionname, the parameters of the function call: Float/int/string/object (which looks at the function definition), time (event trigger)

Practical application: This event mechanism can perform some events while broadcasting to certain frames. Then we can, in some on-demand some special effects, in a certain voice on the demand, at some point to do some screen effects, at some point to the enemy "Knockback" "Hit Fly" "Hit floating empty", very conducive to achieve a variety of rhythm effect! You can also add a skill break mechanism: when you play between 2 frames, you can be interrupted: for example, I'm jumping off a player and I'm interrupted in the air and I'm bounced back. This kind of effect must be very cool. The reader can define various functions that accept events in a specialized script and handle them to use the event mechanism.

2.AnimationCurve:

Unity Manual section:

Add curve, this curve and event is a bit like but different, the event is a few frames, curves is each frame, curves can be used with onanimatormove, such as each frame at different speeds forward:

void Onanimatormove ()        {             = getcomponent<animator>();                        = transform.position;             + = animator. GetFloat ("runspeed"//runspeed is a curves curve variable that controls movement,             Transform.position = newposition;    // A similar approach allows the character to animate a variety of movements.          }

An example is given in the official documentation:

For example, when people breathe in a cold environment, the water mist of the breath is controlled by the particle system, so that the current values can be controlled by the Animator.get parameter (a curve name) when the breath animation or standing animation is made, controlling the water mist size.

  

Unity Script section:

Properties: Keys (Keyframe key collection), length (the num of keys), Postwrapmode (last frame loop type), Prewrapmode (first frame loop type), This[int] Get keyframe

Interface: Evaluate (time): Calculates the value of a curve at

Summary: Curves feeling is: side of the animation side of the other things to do, the event is like a few frames to do something. 2 Use it to make your animation system rich, use these two small sharp weapon.

3.Animation layers and matte implementation: eat apples while walking

1. To eat Apple animation mask: In the Animation tab mask in the mask, only tick selected portions of the bones to eat apples, 3 cases add:

A. Add an existing mask file, you can create a mask from the Assets->create->avatar mast
B. If you are animating a human-like model, use the click-to-Go mask
C. If it is an animation of a generic model, tick the joint and mask

  

2. Create a new Layer:eatapple Layer, drag the 1th step mask into the Mask parameter here, set the layer setting higher than the Walk animation layer, and set the blending of the layer to override, so that The animation of walking and eating apple can be carried out at the same time and the Apple animation override the upper body animation of walking animation.

  

4.Animator Override Controller:

As the name implies, it is the override "Animator controller", first simply say animations controller:

Animation Controller can think of it is the animation state machine, animator animation system is controlled by animator Controller to play the animation, there is a reference to each animation segment Animation clip, And the logic that plays the animation, such as state transitions.

While the animator Override controller is used to expand an existing Animtor controller, it is only on the latter basis in some states to broadcast new animation clip only, to maintain the latter state machine logic, structure and so on: retaining the Original ' s structure, parameters and logic.

So in the application, Animator Override controller can do a state machine (Animator Controller) to achieve multiple sets of actions, very conducive to maintenance. As the official saying is:

If a class of models can share the animation state machine, then you can get a basic animtor Controller, which is basically the animation state logic, and then to use a model to get a animtor Override controller, Then the different animation clip to their own, so that just maintain an animated state machine, worry and effort. For example, NPC system of each NPC. Many monsters can also be shared, not to mention the protagonist.

5.1 Kinds of System for Dressup

(Review a point of knowledge: skinning skin: Attach a mesh to the skeleton, bind the vertices in the mesh to the bone)

First load 2 models out, the 1th model is the basic model, skinmesh/animation/animator and so on, the 2nd model is a nearly empty model with Skinmesh, the first one is the source model, the second is the target model, All we have to do now is replace the 1th model's Skinmesh with the Skinmesh of the 2nd model, as follows:

A. Get the Skinmesh Old_meshrender of the first model, get the Skinmesh Dst_meshrender of the 2nd model, and get all the bones of the first model transform[] bones.

B. The goal is to obtain a list of the individual bones in the dst_meshrender that map to bones, that is, to find the corresponding bone in the 1th model for each vertex in the Dst_meshrender mesh, to reassign the bones to "new skin" and then to Old_ Meshrender's grid Sharemesh replaced Dst_meshrender's Sharemesh, Old_meshrender's skeleton bones into a new skeleton list (of course, the 1th model), in short, is: to reassign the mesh for the 1th model (the second model), because the mesh is new, it is necessary to remap the mesh vertices to the bone, and the code may be easier to understand:

Skinnedmeshrender dst_meshr = newmodel.getcomponent<skinnedmeshrender>(); Skinnedmeshrender old_meshr= oldmodel.getcomponent<skinnedmeshrender>(); Transform[] Bones=Old_meshr.bones; Transform[] Newbones=NewTransform[dst_meshr.bones.length];  for(inti =0; I < dst_meshr.bones.length;++i) { for(intj =0; J < Old_meshR.bones.Length; ++j) {if(Old_meshr.bones[j].name = =dst_meshr.bones[i].name) {Newbones[i]=Old_meshr.bones[j];  Break; } }} Old_meshr.sharedmesh=Dst_meshr.sharedmesh; Old_meshr.bones=Newbones; Old_meshr.sharedmaterials= Dst_meshr.sharedmaterials;

The principle of the skin-changing technology is clear, simple, and easy to use, and only the skin (mesh) does not change the model skeleton, so it is very convenient for the animation system, just to bind those things to the original model can be.

However, it is important to note that the skeleton of each model should be standardized, as far as possible skeleton with the original model consistent, at least to achieve the skeleton "can not be less."

6. Mixed tree

State transitions and mixed trees, although both are used to make smooth animations, but the difference is large (readers can only look at the first 3 points):
1. State transfer is a smooth transition from one animation state to another animation state, 2 states of the animation can be distinguished very large also behaved beautifully, can not maintain the middle State, that can not produce new animation let you broadcast; is part of the animation state machine, is a process.
2. The animated blend tree is a mixture of multiple animations:
Blend multiple animations smoothly by incorporating parts of them all to varying degrees
Mixing different parts of a character, for one part, is to mix the angles of that part of all animations, forming a new angle for that part, and a blended animation.
There are parameters to control the mixing weights of each animation to achieve the goal (such as turning left: the right to start is significant, the weight to the left is small, then ...) )
To make the mixing effect good, each animation needs a similar shape, this and the transfer of different; animation mixing tree as a special state exists in the animation state machine, is a state, a new state.
3. In view of the above description, it is important to note that the sub-animation of the hybrid tree:
Examples of similar motions could be various walk and run animations. In order for the blend to work well, the movements in the clips must take place at the same points in normalized time. For example, walking and running animations can is aligned so that's the moments of contact in foot to the floor take place At the same points-normalized time (e.g. the left foot hits at 0.0 and the ' right ' foot at 0.5). Since normalized time is used, it doesn ' t matter if the clips is of different length.
My understanding is that walking animation and run animation are in 0.7 when the right foot touch the ground, or 0.7 of the time after fusion, right foot in the air, that is, the movement in some key points such as off the ground and touch the ground. The percentage on the timeline should be similar, on this basis, the length of each action is not considered, such as run is shorter than walk, this does not matter.
4. To create a hybrid tree, select a state->create. Blend tree, double-click on the blend tree to enter the mixed Trees edit page, and the inspector on the right is the Anim clips and parameters that control the blending.
5. Some parameters: There is a compute thresholds parameter, can help to specify a variety of threshold control mix. such as speed in the x direction.
6. Add the created parameters to the hybrid tree: Manually type in the parameter selection bar, enter! This is the same as adding parameters to the animation state machine
7. The above 4 is for one-dimensional mixing, two-dimensional mixing is generally not used, not discussed, multi-dimensional mixed pretty interesting, can be used to control facial expressions, multidimensional mixing practices (that control facial expressions do example): Give a number of animation, each animation corresponding to a parameter control the weight of the mixing, so that by controlling the parameters can "produce "A lot of interesting emoticons.

8. Never know how to move. Browse the respective states of the animator window: ALT + left mouse button!

9.animator of Targetmatching Technology

If you have a jumping animation, to achieve the effect of jumping to an object, you can consider using animator's targetmatching technique:

Get to the model of animator ANI, and then

Animator. Matchtarget (Jumptarget.position, Jumptarget.rotation, Avatartarget.leftfoot, new Matchtargetweightmask (Vector3.one , 1f), 0.141f, 0.78f);
0.14, and 0.78 is the percentage of takeoff and skip time
Note: I did not test success, did not see the effect of jumping to an object, =. =, maybe the wrong step.

10.IK Controlling bones

Steps:
1. Model rig's animation type selected humanoid, animation state machine Controller's layer hook on IK Pass;
2. Write a script to hang onto the model prefab, open the Void Onanimatorik () function, and control IK in this function:
Concrete look at the inversekinematics.html of manual: Control model steering and rotation and position of various parts
Note: Has been tested, the effect is also interesting, may be used in some interesting places, perhaps the project would like to do a series of controllable animation will use it, otherwise, the direct production of animation to solve.

11. Root Movement

The root movement is the animation itself, such as walking animation, if not apply root motion "in situ" (in-place), if the application will walk forward.

In the actual project, in general, do not apply root motion, and through the code to set transform Postion, in summary:

1. Do not apply root motion and the animator component is located on the obj control script does not implement Onanimatormove, and all scripts do not control the model position,=> model in situ;
2. Do not apply root motion and the animator component is located on the obj control script does not implement Onanimatormove, but other scripts control the model position,=> model is not in situ;
The control script on the obj of the 3.animator component is implemented Onanimatormove (this time the Apply root motion is not selectable), and all scripts do not control the model position,=> model is not in place, and Onanimatormove control;
The control script on the obj of the 4.animator component is implemented Onanimatormove (this time the Apply root motion is not selectable), and other scripts control the model position,=> model is not in place, That is controlled by other scripts and controlled by Onanimatormove;
5.apply Root Motion (4 can not hang Onanimatormove script), and all scripts do not control the model position,=> model is not in place, according to the animation of the Reagan movement;
6.apply Root Motion (4 can not be hung with onanimatormove script), and other scripts control the model position,=> model is not in place, according to the motion of the Reagan Motion is also controlled by other scripts;

PS1: The above only mention the position, the root movement also includes the rotation, and the control logic is the same.

Official Note: Root motion is the effect where a object ' s entire mesh moves away from its starting point and that motion is created By the animation itself rather than by changing the Transform position. Note that applyrootmotion have no effect when the script implements a Monobehaviour.onanimatormove function. Changing the value of Applyrootmotion at runtime would re-initialize the animator.

11. Some optimization recommendations

1.The Animator doesn ' t spend time processing when a Controller isn't set to it
2. Use less scaling animation curves
3.When Importing humanoid animation use a bodymask to remove IK goals or fingers animation if they is not needed.
If it's a human-like animation, use a body mask to remove IK goals and unwanted detail animations (such as finger animations)
4.When you use Generic, using the root motion is more than expensive than not using it. If your animations don ' t use root motion, make sure, then you have no root bone selected.
If you choose General mode, do not use root motion, do not select root bone
5.Use hashes instead of strings to query the Animator. Use Hashid instead of string to get animation state or parameters, the play function of the animation overloads the hash and string, you can consider a mapping, string->hash, using hash to get the parameters (broadcast animation), To replace String.
6. Use curves to achieve some extra performance: When you play the animation, you do things at the same time.
7.Rig optimize gameobjects (select Create from the This model is available)

12.Animator Component

Unity Manual: Refer to this article: http://www.cnblogs.com/Tearix/p/6941156.html

Unity Script: Too many properties and interfaces, look at the official documents, there is a translation can refer to, but that is only the author's own understanding, not guarantee accurate:

Http://www.cnblogs.com/hont/p/5100472.html?utm_source=tuicool&utm_medium=referral

13. A pit

Play (A); Play (B); it ended up playing a; it was as if there had been a chance to not play an animation, and did not find out if that was the case. The workaround is to:

Play (A); Update (x); Play (b); The final play is B, and you can overwrite it with update. If it is crossfade, the situation is more complicated, and this question I specifically and a netizen discussed, not mentioned here.

Unity's study of animator animation system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.