The Mecanim animation system is capable of applying Root Motion to characters. In other words, it actually moves the character according to the animation clip, as opposed to arbitrarily translating the character model while playing an in-place animation cycle. This makes most Mixamo animation clips perfect for use with Mecanim.
Another new feature for the animation system is Blend Trees, which can make transitions between animation states smooth and easy. In this recipe, we will take advantage of these new features to make our character walk and run forwards or backwards, and also strafe right and left, at different speeds.
For this recipe, we have prepared a project named MixamoProject
, containing several assets such as levels, animated characters, and props. You can find it inside the 0423_05_codes
folder.
To apply Root Motion to your character using Blend Trees, perform the following steps:
0
, 0.9
, 0
); change the entry in the Radius field to 0.34
and the Height field to 1.79
:xSpeed
, zSpeed
, and Speed
.-1
and 1
. Also, use the button with the + sign to add three new Blend Trees. From top to bottom, rename them as StrafeLeft
, WalkRun
, and StrafeRight
.-2
and 2
, and use the button with the + sign to add five Motion fields.-2
and 0
; use the button with the + sign to add three Motion fields.0
and 2
; use the button with the + sign to add three Motion fields.2
and click on the play icon in the Preview window. That should give you an idea on how the transition will be made.BasicController02
.using UnityEngine; using System.Collections; public class BasicController02 : MonoBehaviour { private Animator animator; private CharacterController controller; public float transitionTime = 0.25f; void Start () { controller = GetComponent<CharacterController>(); animator = GetComponent<Animator>(); if(animator.layerCount >= 2) animator.SetLayerWeight(1, 1); } void Update () { float accelerator = 1.0f; if(controller.isGrounded){ if (Input.GetKey (KeyCode.RightShift) ||Input.GetKey (KeyCode.LeftShift) ){ accelerator = 2.0f; } else if(Input.GetKey (KeyCode.RightAlt) ||Input.GetKey (KeyCode.LeftAlt) ){ accelerator = 1.5f; } else { accelerator = 1.0f; } float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); float xSpeed = h * accelerator; float zSpeed = v * accelerator; animator.SetFloat("xSpeed", xSpeed, transitionTime, Time.deltaTime); animator.SetFloat("zSpeed", zSpeed, transitionTime, Time.deltaTime); animator.SetFloat("Speed", Mathf.Sqrt(h*h+v*v), transitionTime, Time.deltaTime); //transform.Rotate(Vector3.up * (Time.deltaTime * v * Input.GetAxis("Mouse X") * 90), Space.World); } } }
Shift
key or walk faster by pressing Alt.Our script will detect the user's keyboard input and translate it into variables to be passed to the Animator Controller as its parameters Speed, xSpeed, and vSpeed. These parameters will determine which animation clips should be played. For instance, if the player presses the down arrow and Shift keys together, the parameters Speed, xSpeed, and vSpeed will be passed with the values 1
, 0
, and -2
respectively. A positive value for Speed will trigger the transition from the Idle state to the Move state. Furthermore, an xSpeed value of 0
, indicating that the left and right arrow keys are not being pressed, will favor the WalkRun Blend Tree and not StrafeLeft and StrafeRight. Finally, the vSpeed parameter (with a value of -2
) will play the walking_backwards motion clip within the WalkRun Blend Tree. The character will then move according to the animation being played. Observe how pressing the Alt key actually blends the animation clips for walking and running in real time.
If you want to learn more about Mecanim's animation system, there are some links you might want to check out, such as Unity's documentation at http://docs.unity3d.com/Documentation/Manual/MecanimAnimationSystem.html.
You can also check out Mecanim Example Scenes, available at the Unity Asset Store at http://u3d.as/content/unity-technologies/mecanim-example-scenes/3Bs and the Mecanim video tutorials, at http://video.unity3d.com/video/7362044/unity-40-mecanim-animation.
3.17.157.6