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 Motion
In 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. With the addition of the root motion feature in legacy to understand 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:
//--to calculate the root Motion of the current frameVector3 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; Notice that we're going to put the m_rootmotion on the role 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:usingUnityengine;usingSystem.Collections; Publicclassapplyrootmotion:monobehaviour{ PublicTransform M_flagobject;//An object used to test the position//--Root Motion control VariablesTransform M_rootbone;    Vector3 M_lastrootpos; Vector3 m_rootmotion;intM_lastanimtime;voidStart () {//--read root from Skinnedmeshrenderer BoneSkinnedmeshrenderer Skinmesh = This.gameobject.getcomponentinchildren<skinnedmeshrenderer> (); M_rootbone = Skinmesh.rootbone;//--Initialization of variablesM_rootmotion = Vector3.zero;  M_lastrootpos = m_rootbone.localposition;  M_lastanimtime = 0; }voidUpdate () {//--Apply Root MotionVector3 Nextpos = This. transform.position + m_rootmotion; This. transform.position = Nextpos;
//--Test Code: Update the location of the test objectVector3 flagpos = m_flagobject.position;  Flagpos.x = nextpos.x;  Flagpos.z = nextpos.z; M_flagobject.position = Flagpos;//--Test code: Update CameraCamera.main.transform.LookAt ( This. Transform); }voidLateupdate () {Animationstate animstate = This. animation["Walking"];if((int) Animstate.normalizedtime > M_lastanimtime) {//--animation loop processingM_lastrootpos = m_rootbone.localposition;  M_rootmotion = Vector3.zero; }Else{//--to calculate the root Motion of the current frameVector3 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/1i3tIO8P


For more information, please visit the "Dog Planing Learning Network" http://unity.gopedu.com

Disclaimer: This document is from the "Dog Planing Learning Network" community, is the Netizen self-published Unity3d study articles, if there is any content violated your relevant interests, please communicate with the official, we will deal with the real-time.




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.