Apply Root Motion and unity3dlegacy to the Unity3D Legacy animation System
Recently, we carefully compared two animation systems in the current Unity3D version: Legacy and Mecanim. The Mecanim system is much more powerful than Legacy, but it is really inconvenient to use AnimatorController (although the use of AnimatorOverrideController can avoid repeated state machine editing ), the reason is that the game logic layer usually uses a state machine or similar mechanism to control the role's status. The state logic at the role level cannot correspond to the animation level one by one, the two complex state machines must be combined... Think about it! No wonder many friends are still using the Legacy animation system. In fact, the Legacy animation system has comprehensive functions, including Layer, transition hybrid, and up-and-down hybrid, which are fully competent and can be controlled directly. I only need Root Motion, which is not supported by features. This article will discuss how to add the Root Motion function to the Legacy animation system, which is actually very simple.
What is Root Motion when Root Motion is not used? The displacement control like walking and running is like this: this is actually not scientific, and the role of program control, it can only be treated as a particle, and most of the time it is a constant motion, and the movement of the role in the animation is often difficult to match with this. Therefore, a relatively good computing and art skills are required to avoid the "slide" Phenomenon of the role. In the fast movement like "run", the slide is better to handle, if it is a slow movement .... Even more powerful art can't help. In this case, it is best to use Root Motion:
After adding the Root Motion function in Legacy to understand the concept of Root Motion, we can easily implement this function in the Unity3D engine. Unity3D has a unified object hierarchy design, which is very good. We can easily find the root skeleton of the role and then read the Transform. See the following sample code:
// -- Calculate the Root Motion Vector3 rootPos = m_rootBone.localPosition of the current frame; m_rootMotion = rootPos-m_lastRootPos; m_lastRootPos = rootPos; rootPos. x = 0; rootPos. z = 0; m_rootMotion.y = 0; m_rootBone.localPosition = rootPos;
Please note that we will add m_rootMotion to the role object in the subsequent code, so the postion of m_rootBone is reset. After reading the Root Motion of this frame, you can apply it to the current object:
//-- Apply Root MotionVector3 nextPos = this.transform.position + m_rootMotion; this.transform.position = nextPos;
In addition, a detail needs to be processed. The frame in the animation loop needs to be specially processed. Okay. Let's take a look at the complete source code:
Using UnityEngine; using System. collections; public class ApplyRootMotion: MonoBehaviour {public Transform m_flagObject; // an object used to test the position // -- Root Motion Control Variable Transform m_rootBone; Vector3 m_lastRootPos; Vector3 m_rootMotion; int m_lastAnimTime; void Start () {// -- read the Root BoneSkinnedMeshRenderer skinMesh = this from SkinnedMeshRenderer. gameObject. getComponentInChildren <SkinnedMeshRenderer> (); m_rootBone = skinMesh. rootBone; // -- variable initialization m_rootMotion = Vector3.zero; m_lastRootPos = m_rootBone.localPosition; m_lastAnimTime = 0;} void Update () {// -- Apply Root MotionVector3 nextPos = this. transform. position + m_rootMotion; this. transform. position = nextPos; // -- test code: update the position of the test object Vector3 flagPos = m_flagObject.position; flagPos. x = nextPos. x; flagPos. z = nextPos. z; m_flagObject.position = flagPos; // -- test code: update the Camera. main. transform. lookAt (this. transform);} void LateUpdate () {AnimationState animState = this. animation ["walking"]; if (int) animState. normalizedTime> m_lastAnimTime) {// -- animation loop processing m_lastRootPos = m_rootBone.localPosition; m_rootMotion = Vector3.zero;} else {// -- calculate the Root Motion Vector3 rootPos = m_rootBone.localPosition of the current frame; m_rootMotion = rootPos-m_lastRootPos; m_lastRootPos = rootPos; rootPos. x = 0; rootPos. z = 0; m_rootMotion.y = 0; m_rootBone.localPosition = rootPos;} m_lastAnimTime = (int) animState. normalizedTime ;}}
And finally... Well, static images do not show any effect. You can download the complete Demo (open in Unity 4.6). The role movement is smooth and smooth.
Please move to Baidu Network Disk: http://pan.baidu.com/s/1o6kJsIe password: osoc