Mecanim Animation System
Mecanim Animation System
Previous Next Mecanim Animation System Unity has a rich and sophisticated animation system called Mecanim. Mecanim provides: Easy workflow and setup of animations on humanoid characters. Animation retargeting - the ability to apply animations from one character model onto another. Simplified workflow for aligning animation clips. Convenient preview of animation clips, transitions and interactions between them. This allows animators to work more independently of programmers, prototype and preview their animations before gameplay code is hooked in. Management of complex interactions between animations with a visual programming tool. Animating different body parts with different logic.
Typical setup in the Visual Programming Tool and the Animation Preview window Mecanim workflow Workflow in Mecanim can be split into three major stages. 1. Asset preparation and import . This is done by artists or animators, with 3rd party tools, such as Max or Maya. This step is independent of Mecanim features. 2. Character setup for Mecanim, which can be done in 2 ways: a. Humanoid character setup . Mecanim has a special workflow for humanoid models, with extended GUI support and retargeting . The setup involves creating and setting up an Avatar and tweaking Muscle definitions . b. Generic character setup . This is for anything like creatures, animated props, four-legged animals, etc. Retargeting is not possible here, but you can still take advantage of the rich feature set of Mecanim, including everything described below. 3 . Bringing characters to life . This involves setting up animation clips , as well as interactions between them, and involves setup of State Machines and Blend Trees , exposing Animation Parameters , and controlling animations from code. Mecanim comes with a lot of new concepts and terminology. If at any point, you need to find out what something means, go to our Animation Glossary . A Glossary of Animation and Mecanim terms Asset Preparation and Import
Preparing your own character Importing Animations Splitting Animations Working with humanoid animations Creating the Avatar Configuring the Avatar Muscle setup Avatar Body Mask Retargeting of Humanoid animations Inverse Kinematics (Pro only) Generic Animations in Mecanim Bringing Characters to Life Looping animation clips Animator Component and Animator Controller Animation State Machines Animation States Animation Transitions Animation Parameters Blend Trees Mecanim Advanced topics Working with Animation Curves in Mecanim (Pro only) Sub-State Machines Animation Layers Animation State Machine Preview (solo and mute) Target Matching Root Motion - how it works Tutorial: Scripting Root Motion for "in-place" humanoid animations Legacy animation system While Mecanim is recommended for use in most situations, especially for working humanoid animations, the Legacy animation system is still used in a variety of contexts. One of them is working legacy animations and code (content created before Unity 4.0). Another is controlling animation clips with parameters other than time (for example for controlling the aiming angle). For information on the Legacy animation system, see this section Unity intends to phase out the Legacy animation system over time for all cases by merging the workflows into Mecanim.
Page last updated: 2012-11-08
Previous Next
Animation Clip Animation data that can be used for animated characters or simple animations. It is a simple "unit" piece of motion, such as (one specific instance of) "Idle", "Walk" or "Run" sub-Asset
Body Mask A specification for which body parts to include or exclude for a skeleton Asset (.mask ) Used in Animation Layers and in the importer Animation Curves Curves can be attached to animation clips and controlled by various parameters from the game Avatar related terms
Avatar An interface for retargeting one skeleton to another sub-Asset Retargeting Applying animations created for one model to another Process Rigging The prcoess of building a skeleton hierarchy of bone joints for your mesh Process done in an external tool, such as Max or Maya Skinning The process of binding bone joints to the character's mesh or 'skin' Process done in an external tool, such as Max or Maya Muscle Definition A Mecanim concept, which allows you to have a more intuitive control over the character's skeleton. When an Avatar is in place, Mecanim works in muscle space, which is more intuitive than bone space T-pose The pose in which the character has his arms straight out to the sides, forming a "T". The required pose for the character to be in, in order to make an Avatar Bind-pose The pose at which the character was modelled
Human template A pre-defined bone-mapping Asset (.ht ) Used for matching bones from FBX files to the Avatar. Animator and Animator Controller related terms Animator Component Component on a model that animates that model using the Mecanim animation system. The component has a reference to an Animator Controller asset that controls the animation. Component Root Motion Motion of character's root, whether it's controlled by the animation itself or externally.
Animator Controller (Asset) The Animator Controller controls animation through Animation Layers with Animation State Machines and Animation Blend Trees, controlled by Animation Parameters. The same Animator Controller can be
referenced by multiple models with Animator components. Asset (.controller ) Animator Controller (Window) The window where the Animator Controller Asset is visualized and edited. Window Animation Layer An Animation Layer contains an Animation State Machine that controls animations of a model or part of it. An example of this is if you have a full-body layer for walking / jumping and a higher layer for upper-body motions such as throwing object / shooting. The higher layers take precedence for the body parts they control. Animation State Machine A graph controlling the interaction of Animation States. Each state references an Animation Blend Tree or a single Animation Clip. Animation Blend Tree Used for continuous blending between similar Animation Clips based on float Animation Parameters. Animation Parameters Used to communicate between scripting and the Animator Controller. Some parameters can be set in scripting and used by the controller, while other parameters are based on Custom Curves in Animation Clips and can be sampled using the scripting API. Inverse Kinematics (IK) The ability to control the character's body parts based on various objects in the world. Non-Mecanim animation terms Animation Component The component needed for non-Mecanim animations Component
Page last updated: 2012-11-07
Stages for preparing a character (modeling, rigging, and skinning) How to obtain humanoid models There are three main ways to obtain humanoid models for with the Mecanim Animation system: Use a procedural character system or character generator such as Poser , Makehuman or Mixamo . Some of these systems will rig and skin your mesh (eg, Mixamo) while others will not. Furthermore, these methods may require that you reduce the number of polygons in your original mesh to make it suitable for use in Unity. Purchase demo examples and character content from the Unity Asset Store . Also, you can of course prepare your own character from scratch. Export & Verify Unity imports a number of different generic and native 3D file formats. The format we recommend for exporting and verifying your model is FBX 2012 since it will allow you to: Export the mesh with the skeleton hierarchy, normals, textures and animation Re-import into your 3D package to verify your animated model has exported as you expected Export animations without meshes Further details The following pages cover the stages of preparing and importing animation assets in greater depth Preparing your own character
Previous Next
The Import Settings Dialog for a mesh See the FBX importer page for a full description of the available import options. Splitting animations (back to Mecanim introduction )
Page last updated: 2012-11-01
Previous Next
You will see a list available clips which you can preview by pressing Play in the Preview Window (lower down in the inspector). The frame ranges of the clips can be edited, if needed. Working with models that have unsplit animations For models where the clips are supplied as one continuous animation, the Animation tab in the Animation Importer Inspector will look like this:
4In cases like this, you can define the frame ranges that correspond to each of the separate animation sequences (walking, jumping, etc). You can create a new animation clip by pressing (+) and selecting the range of frames that are included in it. For example: walk animation during frames 1 - 33 run animation during frames 41 - 57 kick animation during frames 81 - 97
The Import Settings Options for Animation In the Import Settings, the Split Animations table is where you tell Unity which frames in your asset file make up which Animation Clip. The names you specify here are used to activate them in your game.
For further information about the animation inspector, see the Animation Clip component reference page . Adding animations to models that do not contain them You can add animation clips to an Animation component even for models without muscle definitions (ie, non-Mecanim). You need to specify the default animation clip in the Animation property, and the available animation clips in the Animations property. The animation clips you add to such a non-Mecanim model should also be setup in a non-Mecanim way (ie, the Muscle Definition property should be set to None) For models that have muscle definitions (Mecanim), the process is different:Create a New Animator Controller Open the Animator Controller Window Drag the desired animation clip into the Animator Controller Window Drag the model asset into the Hierarchy. Add the animator controller to the Animator component of the asset. Importing Animations using multiple model files Another way to import animations is to follow a naming scheme that Unity allows for the animation files. You create separate model files and name them with the convention '[email protected]'. For example, for a model called "goober", you could import separate idle, walk, jump and walljump animations using files named "[email protected]", "[email protected]", "[email protected]" and "[email protected]". Only the animation data from these files will be used, even if the original files are exported with mesh data.
An example of four animation files for an animated character (note that the .fbx suffix is not shown within Unity) Unity automatically imports all four files and collects all animations to the file without the @ sign in. In the example above, the goober.mb file will be set up to reference idle, jump, walk and wallJump automatically. For FBX files, simply export a model file with no animation ticked (eg, goober.fbx) and the 4 clips as goober@animname .fbx by exporting the desired keyframes for each (enable animation in the FBX dialog). (back to Mecanim introduction )
Page last updated: 2012-11-12
Previous Next
Previous Next
Also, in the case of a successful match, an Avatar sub-asset is added to the FBX asset, which you will be able to see in the project view hierarchy.
The inspector for an Avatar asset If Mecanim was unable to create the Avatar, you will see a cross next to the Configure ... button, and no
Avatar sub-asset will be added. When this happens, you need to configure the avatar manually . Non-humanoid animations Two options for non-humanoid animation are provided: Generic and Legacy. Generic animations are imported using the Mecanim system but don't take advantage of the extra features available for humanoid animations. Legacy animations use the the animation system that was provided by Unity before Mecanim. There are some cases where it is still useful to work with legacy animations (most notably with legacy projects that you don't want to update fully) but they are seldom needed for new projects. See this section of the manual for further details on legacy animations. (back to Avatar Creation and Setup ) (back to Mecanim introduction )
Page last updated: 2012-11-07
Previous Next
You can either adjust individual bones in the body (lower part of the view) or manipulate the character using predefined deformations which operate on several bones at once (upper part of the view). Muscle Clips In the Animation tab, you can set up Muscle Clips, which are animations for specific muscles and muscle groups.
You can also define which body parts these muscle clips apply to.
Previous Next
Body mask in the Body Mask inspector (arms excluded) In the Animation tab of the mesh import inspector, you will see a list entitled Clips that contains all the object's animation clips. When you select an item from this list, options for the clip will be shown, including the body mask editor. You can also create Body Mask Assets (Assets->Create->Avatar Body Mask), which show up as .mask files on disk. The BodyMask assets can be reused in Animator Controllers , when specifying Animation Layers
A benefit of using body masks is that they tend to reduce memory overheads since body parts that are not active do not need their associated animation curves. Also, the unused curves need not be calculated during playback which will tend to reduce the CPU overhead of the animation. (back to Mecanim introduction )
Page last updated: 2012-10-18
Previous Next
To set up IK for a character, you typically have objects around the scene that a character interacts with, and then set up the IK thru script, in particular, Animator functions like SetIKPositionWeight , SetIKRotationWeight , SetIKPosition , SetIKRotation , SetLookAtPosition , bodyPosition , bodyRotation In the illustration above, we show a character grabbing a cylindrical object. How do we make this happen? We start out with a character that has a valid Avatar, and attach to it a script that actually takes care of the IK, let's call it IKCtrl: using UnityEngine; using System; using System.Collections; [RequireComponent(typeof(Animator))] public class IKCtrl : MonoBehaviour { protected Animator animator; public bool ikActive = false; public Transform rightHandObj = null;
void Start () { animator = GetComponent<Animator>(); } //a callback for calculating IK void OnAnimatorIK() { if(animator) { //if the IK is active, set the position and rotation directly to the goal. if(ikActive) { //weight = 1.0 for the right hand means position and rotation will be at the IK goal (the place the character wants to grab) animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1.0f); animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1.0f); //set the position and the rotation of the right hand where the external object is if(rightHandObj != null) { animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position); animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation); } } //if the IK is not active, set the position and rotation of the hand back to the original position else { animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0); animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0); } } } } As we do not intend for the character to grab the entire object with his hand, we position a sphere where the hand should be on the cylinder, and rotate it accordingly. This sphere should then be placed as the "Right Hand Obj" property of the IKCtrl script
Observe the character grabbing and ungrabbing the object as you click the IKActive checkbox (back to Mecanim introduction )
Previous Next
Root node in generic animations While in the case of humanoid animations, we have the knowledge about the center of mass and orientation, in the case of Generic animations, the skeleton can be arbitrary, and we need to specify a reference bone, or the "root node". Selecting the root node allows us to establish correspondence between animation clips for a generic model, and blend properly between animations that are not "in place". The root node is also essential for separating animation of bones relative to reach other and motion of the root in the world (controlled from OnAnimatorMove )
Page last updated: 2012-11-06
Previous Next
Clip ranges with good match for Loop Pose Once the loop match indicator is green, Enabling Loop Pose (for example) will make sure the looping of the pose is artifact-free. For more details on animation clip options, see Animation Clip reference (back to Mecanim introduction )
Page last updated: 2012-11-12
Previous Next
The Animator component references an Animator Controller which is used for setting up behavior on the character. This includes setup for State Machines , Blend Trees , and events to be controlled from script. Properties Controller The animator controller attached to this character Avatar The Avatar for this character. Apply Root Motion Should we control the character's position from the animation itself or from script. Animate Physics Should the animation interact with physics? Culling Mode Culling mode for animations Always animate Always animate, don't do culling Based on Renderers When the renderers are invisible, only root motion is animated. All other body parts will remain static while the character is invisible. Animator Controller You can view and set up character behavior from the Animator Controller view (Menu: Window > Animator Controller). An Animator Controller can be created from the Project View (Menu: Create > Animator Controller). This creates a .controller asset on disk, which looks like this in the Project Browser
Animator Controller asset on disk After the state machine setup has been made, you can drop the controller onto the Animator component
The Animator Controller Window The Animator Controller Window will contain The Animation Layer Widget (top-left corner, see Animation Layers ) The Event Parameters Widget (bottom-left, see Animation Parameters ) The visualization of the State Machine itself . Note that the Animator Controller Window will always display the state machine from the most recently selected .controller asset, regardless of what scene is currently loaded. (back to Mecanim introduction )
Page last updated: 2012-10-18
Previous Next
The importance of state machines for animation is that they can be designed and updated quite easily with relatively little coding. Each state has an animation sequence associated with it that will play whenever the machine is in that state. This enables an animator or designer to define the possible sequences of character actions and animations without being concerned about how the code will work. Mecanim state machines Mecanim's Animation State Machines provide a way to overview all of the animation clips related to a particular character and allow various events in the game (for example user input) to trigger different animations. Animation State Machines can be set up from the Animator Controller Window , and they look something like this:
State Machines consist of States, Transitions and Events and smaller Sub-State Machines can be used as components in larger machines. Animation States Animation Transitions Animation Parameters (back to Mecanim introduction )
Page last updated: 2012-11-02
Previous Next
Speed The default speed of the animation Motion The animation clip assigned to this state Foot IK Should Foot IK be respected for this state Transitions The list of transitions originating from this state The default state, displayed in brown, is the state that the machine will be in when it is first activated. You can change the default state, if necessary, by right-clicking on another state and selecting Set As Default from the context menu. The solo and mute checkboxes on each transition are used to control the behaviour of animation previews - see this page for further details. A new state can be added by right-clicking on an empty space in the Animator Controller Window and selecting Create State->Empty from the context menu. Alternatively, you can drag an animation into the Animator Controller Window to create a state containing that animation. (Note that you can only drag Mecanim animations into the Controller - non-Mecanim animations will be rejected.) States can also contain Blend Trees . Any State Any State is a special state which is always present. It exists for the situation where you want to go to a specific state regardless of which state you are currently in. This is a shorthand way of adding the same outward transition to all states in your machine. Note that the special meaning of Any State implies that it cannot be the end point of a transition (ie, jumping to "any state" cannot be used as a way to pick a random state to enter next).
Previous Next
Previous Next
Parameters can be assigned values from a script using functions in the Animator class: SetVector , SetFloat , SetInt , and SetBool . Here's an example of a script that modifies parameters based on user input using UnityEngine; using System.Collections; public class AvatarCtrl : MonoBehaviour { protected Animator animator; public float DirectionDampTime = .25f; void Start () { animator = GetComponent<Animator>(); } void Update () { if(animator) { //get the current state AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0); //if we're in "Run" mode, respond to input for jump, and set the Jump parameter accordingly. if(stateInfo.nameHash == Animator.StringToHash("Base Layer.RunBT")) { if(Input.GetButton("Fire1")) animator.SetBool("Jump", true ); }
else { animator.SetBool("Jump", false); } float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); //set event parameters based on user input animator.SetFloat("Speed", h*h+v*v); animator.SetFloat("Direction", h, DirectionDampTime, Time.deltaTime); } } } (back to Animation State Machines )
Page last updated: 2012-11-09
Previous Next
In the inspector, the first thing you need is to select the Animation Parameter that will control this Blend Tree. In this example, the parameter is direction which varies between -1.0 (left) and +1.0 (right), with 0.0 denoting a straight run without leaning. Then you can add individual animations by clicking + -> Add Motion Field to add an animation clip to the blend tree. When you're done, it should look something like this:
The lines in the diagram at the top of the inspector show the proportion of each animation that is incorporated as the parameter varies between its minimum and maximum values.
If you click and hold down the left mouse button on a line or the space beneath, the animation that it corresponds to will be highlighted in the list below. You can also drag the line left and right to change the parameter range over which it influences the animation (this also performs a live update of the Threshold values in the inspector). The Automate Thresholds checkbox will distribute the clips' thresholds evenly across the numeric range of the parameter. For example, if there are five clips and the parameter ranges from -90 to +90, the thresholds will be set to -90, -45, 0, +45 and +90 in order. The Compute Thresholds buttons will set the thresholds from speed data obtained from the animation. Say, for example, you had a walk animation that covered 1.5 units per second, a jog at 2.3 units per second, and a run at 4 units per second, clicking the Average button would set the parameter range and thresholds for the three animations based on these values. So, if you set the speed parameter to 3.0, it would blend the jog and run with a slight bias toward the jog. The Angular button performs a similar calculation but based on angular speed (degrees per second) rather than linear speed. You can alter the "natural" speed of the animation clips using the time scaling text boxes (the column next to the threshold values), so you could make the walk twice as fast by using a value of 2.0 as its scale. The Adjust Time Scale > Homogenized button rescales the speeds of the clips so that they correspond with the chosen minimum and maximum values of the parameter but keep the same relative speeds they initially had. The red vertical bar indicates the value of the event parameter. If you press Play in the Animation Preview panel and drag the bar left and right, you can see how the value of the parameter is controlling the blending of the different animations. In the Animation view, a diagram of the blend tree complements the information shown in the inspector.
This gives a graphical display of how the animations are combined as the parameter value changes (as you drag the slider, the arrows from the tree root change their shading to show the dominant animation clip). (back to Mecanim introduction )
Page last updated: 2012-11-08
Previous Next
Previous Next
The curves on animation clips in Mecanim The curve's X-axis represents normalized time and always ranges between 0.0 and 1.0 (corresponding to the beginning and the end of the animation clip respectively, regardless of its duration).
Unity Curve Editor Double-clicking an animation curve will bring up the standard Unity curve editor (see Editing Value Properties for further details) which you can use to add keys to the curve. Keys are points along the curve's timeline where it has a value explicitly set by the animator rather than just using an interpolated value. Keys are very useful for marking important points along the timeline of the animation. For example, with a walking animation, you might use keys to mark the points where the left foot is on the ground, then both feet on the ground, right foot on the ground, etc. Once the keys are set up, you can move conveniently between key frames by pressing the Previous/Next Key Frame buttons. This will move the vertical red line and show the normalized time at the keyframe; the value you enter in the text box will then set the value of the curve at that time. Animation Curves and Animator Controller parameters If you have a curve with the same name as one of the parameters in the Animator Controller , then that parameter will take its value from the value of the curve at each point in the timeline. For example, if you make a call to GetFloat from a script, the returned value will be equal to the value of the curve at the time the call is made. Note that at any given point in time, there might be multiple animation clips attempting to set the same parameter from the same controller. In that case, the curve values from the multiple animation clips are blended. If an animation has no curve for a particular parameter then the blending will be done with the default value for that parameter. (back to Mecanim introduction )
Previous Next
The sequence of states in a "Trickshot" action Although this is useful for control purposes, the downside is that the state machine will become large and unwieldy as more of these complex actions are added. You can simplify things somewhat just by separating the groups of states visually with empty space in the editor. However, Mecanim goes a step further than this by allowing you to collapse a group of states into a single named item in the state machine diagram. These collapsed groups of states are called Sub-state machines. You can create a sub-state machine by right clicking on an empty space within the Animator Controller window and selecting Create Sub-State Machine from the context menu. A sub-state machine is represented in the editor by an elongated hexagon to distinguish it from normal states.
A sub-state machine When you double-click the hexagon, the editor is cleared to let you edit the sub-state machine as though it were a completely separate state machine in its own right. The bar at the top of the window shows a "breadcrumb trail" to show which sub-state machine is currently being edited (and note that you can create sub-state machines within other sub-state machines, and so on). Clicking an item in the trail will focus the editor on that particular sub-state machine. The "breadcrumb trail" External transitions As noted above, a sub-state machine is just a way of visually collapsing a group of states in the editor,
so when you make a transition to a sub-state machine, you have to choose which of its states you want to connect to.
Choosing a target state within the "Trickshot" sub-state machine You will notice an extra state in the sub-state machine whose name begins with Up .
The "Up" state The Up state represents the "outside world", the state machine that encloses the sub-state machine in the view. If you add a transition from a state in sub-state machine to the Up state, you will be prompted to choose one of the states of the enclosing machine to connect to.
Connecting to a state in the enclosing machine (back to State Machines introduction ) (back to Mecanim introduction )
Page last updated: 2012-11-07
Previous Next
You can add a new layer by pressing the + on the widget. On each layer, you can specify the body mask (the part of the body on which the animation would be applied), and the Blending type. Override means information from other layers will be ignored, while Additive means that the animation will be added on top of previous layers. The Mask property is there to specify the body mask used on this layer. For example if you want to use upper body throwing animations, while having your character walk or run, you would use an upper body mask, like this:
For more on Avatar Body Masks, you can read this section Animation Layer syncing (Pro only) Sometimes it is useful to be able to re-use the same state machine in different layers. For example if you want to simulate "wounded" behavior, and have "wounded" animations for walk / run / jump instead of the "healthy" ones. You can click the Sync checkbox on one of your layers, and then select the layer you want to sync with. The state machine structure will then be the same, but the actual animation clips used by the states will be distinct.
Previous Next
Soloed transitions will be shown in green, while muted transitions in red, like this:
In the example above, if you are in State 0 , only transitions to State A and State B will be available. The basic rule of thumb is that if one Solo is ticked, the rest of the transitions from that state will be muted. If both Solo and Mute are ticked, then Mute takes precedence. Known issues: The controller graph currently doesn't always reflect the internal mute states of the engine. (back to State Machines introduction ) (back to Mecanim introduction )
Page last updated: 2012-10-08
Previous Next
Find the place in the animation clip at which the character is about to land on his feet, note in this case the value is 78.0% or 0.78.
Create a script (TargetCtrl.cs ) that makes a call to MatchTarget , like this: using UnityEngine; using System; [RequireComponent(typeof(Animator))] public class TargetCtrl : MonoBehaviour { protected Animator animator; //the platform object in the scene 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); } } } Attach that script onto the Mecanim model.
The script will move the character so that it jumps from its current position and lands with its left foot at the target. Bear in mind that the result of using MatchTarget will generally only make sense if it is called at the right point in gameplay. (back to Mecanim introduction )
Page last updated: 2012-11-08
Previous Next
The circle below the character represents the root transform Animation Clip Inspector The Animation Clip Editor settings (Root Transform Rotation, Root Transform Position (Y) and Root Transform Position (XZ)) let you control the Root Transform projection from the Body Transform. Depending on these settings some parts of the Body Transform may be transferred Root Transform. For example you can decide if you want the motion Y position to be part of the Root Motion (trajectory) or part of the pose (body transform), which is known as Baked into Pose.
Root Transform Rotation Bake into Pose: The orientation will stay on the body transform (or Pose). The Root Orientation will be constant and delta Orientation will be identity. This means the the Game Object will not be rotated at all by that AnimationClip. Only AnimationClips that have similar start and stop Root Orientation should use this option. You will have a Green Light in the UI telling you that an AnimationClip is a good candidate. A suitable candidate would be a straight walk or a run. Based Upon: This let you set the orientation of the clip. Using Body Orientation, the clip will be oriented to follow the forward vector of body. This default setting works well for most Motion Capture (Mocap) data like walks, runs, and jumps, but it will fail with motion like strafing where the motion is perpendicular to the body's forward vector. In those cases you can manually adjust the orientation using the Offset setting. Finally you have Original that will automatically add the authored offset found in the imported clip. It is usually used with Keyframed data to respect orientation that was set by the artist. Offset: used to enter the offset when that option is chosen for Based Upon. Root Transform Position (Y) This uses the same concepts described in Root Transform Rotation. Bake Into Pose: The Y component of the motion will stay on the Body Transform (Pose). The Y component of the Root Transform will be constant and Delta Root Position Y will be 0. This means that this clip wont change the Game Object Height. Again you have a Green Light telling you that a clip is a good candidate for baking Y motion into pose. Most of the AnimationClips will enable this setting. Only clips that will change the GameObject height should have this turned off, like jump up or down. Note: the Animator.gravityWeight is driven by Bake Into Pose position Y. When enabled, gravityWeight = 1 , when disable = 0 . gravityWeight is blended for clips when transitioning between
states. Based Upon: In a similar way to Root Transform Rotation you can choose from Original or Mass Center (Body). There is also a Feet option that is very convenient for AnimationClips that change height (Bake Into Pose disabled). When using Feet the Root Transform Position Y will match the lowest foot Y for all frames. Thus the blending point always remains around the feet which prevents floating problem when blending or transitioning. Offset: In a similar way to Root Transform Rotation, you can manually adjust the AnimationClip height using the Offset setting. Root Transform Position (XZ) Again, this uses same concepts described in Root Transform Rotation and Root Motion Position (Y). Bake Into Pose will usually be used for Idles where you want to force the delta Position (XZ) to be 0. It will stop the accumulation of small deltas drifting after many evaluations. It can also be used for a Keyframed clip with Based Upon Original to force an authored position that was set by the artist. Loop Pose Loop Pose (like Pose Blending in Blend Trees or Transitions) happens in the referential of Root Transform. Once the Root Transform is computed, the Pose becomes relative to it. The relative Pose difference between Start and Stop frame is computed and distributed over the range of the clip from 0100%. Generic Root Motion and Loop Pose. This works in essentially the same as Humanoid Root Motion, but instead of using the Body Transform to compute/project a Root Transform, the transform set in Root Node is used. The Pose (all the bones which transform below the Root Motion bone) is made relative to the Root Transform.
Page last updated: 2012-11-09
Previous Next
Preview the animation in the animation viewer to make sure the beginning and the end of the animation align smoothly, and that the character is moving "in-place" On the animation clip create a curve that will control the speed of the character (you can add a curve from the Animation Import inspector Curves-> +) Name that curve something meaningful, like "Runspeed"
Create a new Animator Controller, (let's call it RootMotionController) Drop the desired animation clip into it, this should create a state with the name of the animation (say Run) Add a parameter to the Controller with the same name as the curve (in this case, "Runspeed")
Select the character Dude in the Hierarchy, whose inspector should already have an Animator component. Drag RootMotionController onto the Controller property of the Animator If you press play now, you should see the "Dude" running in place Finally, to control the motion, we will need to create a script (RootMotionScript.cs), that implements the OnAnimatorMove callback. using UnityEngine; using System.Collections; [RequireComponent(typeof(Animator))] public class RootMotionScript : MonoBehaviour { void OnAnimatorMove() { Animator animator = GetComponent<Animator>(); if (animator) { Vector3 newPosition = transform.position; newPosition.z += animator.GetFloat("Runspeed") * Time.deltaTime; transform.position = newPosition; } } } Attach RootMotionScript.cs to "Dude"
Note that the Animator component detects there is a script with OnAnimatorMove and Apply Root Motion property shows up as Handled by Script
Now you should see that the character is moving at the speed specified. (back to Mecanim introduction )
Page last updated: 2012-11-07
Previous Next