Application of root motion in Unity3d's legacy animation system

Source: Internet
Author: User

Two sets of animation systems in the current version of Unity3d have been carefully compared: legacy and Mecanim. Mecanim system functions are much more powerful than legacy, but it is inconvenient to use Animatorcontroller (although using animatoroverridecontroller can avoid repetitive editing of state machines), is because the game logic level often has to use a state machine or similar mechanism to control the status of the role, and the role level of the state logic and animation level can not be one-to-one correspondence, two sets of complex state confidential together ... If you think about it, you feel the egg hurts! No wonder many friends are still using the legacy animation system. Legacy animation System In fact, the function is also very comprehensive, including layer, transition mix, upper and lower body mix and other functions fully capable, and control up directly more. Only root motion This I really need features without support, this article explores how to attach the root motion function on top of the Legacy animation system, which is actually very simple.

What is root MotionIn the case of not using root motion, the displacement control like walking and running is like this:
    1. Please remove the displacement when the animation is exported;
    2. In the program code control the speed of the character movement, while playing the animation, the calculation of its displacement.
This practice is actually very unscientific, the role of program control can only be treated as a particle, and most of the time is uniform motion, and animation of the role of the movement is often difficult to match with this. Therefore, it is necessary to have better calculation and better art skills to avoid the phenomenon of "slipping" of characters. In the "run" this fast moving this, slippery step is also relatively good processing, if it is slow moving .... No great art can help. In this case, it is best to use root Motion:
    1. The art is accompanied by displacement when the animation is exported;
    2. The program will be the animation of each frame of the displacement is read from the animation, and then applied to the character, so that the animation and displacement to achieve a perfect match.

Adding the root motion feature in legacyHaving understood the concept of root motion, we can easily implement this feature in the Unity3d engine. Unity3d has a unified object hierarchy design, which is very good, we can easily find the role of the root skeleton, and then to read the transform transformation, see the following sample code:
--Calculates the current frame's root Motion        Vector3 rootpos = m_rootbone.localposition;        M_rootmotion = Rootpos-m_lastrootpos;        M_lastrootpos = Rootpos;        rootpos.x = 0;        rootpos.z = 0;        M_ROOTMOTION.Y = 0;        M_rootbone.localposition = Rootpos;
Note that we want to attach the m_rootmotion to the character object in the subsequent code, so M_rootbone's postion is reset. After reading the frame's root Motion, it can be applied to the current object:
--Apply Root MotionVector3 nextpos = this.transform.position + m_rootmotion;        This.transform.position = Nextpos;
In addition, a detail needs to be dealt with, in the animation cycle of the frame, the need for special treatment. OK, 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 () {//--reads root from skinnedmeshrenderer boneskinnedmeshrenderer Skinmesh =        This.gameobject.getcomponentinchildren<skinnedmeshrenderer> ();        M_rootbone = Skinmesh.rootbone;        --Initialization of variables 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 Camera Camera.main.transform.LookAt (This.transform);} void Lateupdate () {animationstate AnimState = this.animation["Walking"];if ((int) animstate.normalizedtime > M_lastanimtime) {//--animation loop processing M_last        Rootpos = m_rootbone.localposition;        M_rootmotion = Vector3.zero;        } else {//--calculates the current frame's root Motion Vector3 rootpos = m_rootbone.localposition;        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; }}

The last is ... OK, the static picture does not show the effect, you can download the full demo (use the Unity 4.6 version opens), the character movement is very smooth, no slip step.
Please visit Baidu Network disk: http://pan.baidu.com/s/1o6kJsIe Password: OSOC



Application of root motion in Unity3d's legacy 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.