- Home /
Handling Build-up of Root Motion Errors Over Time
Is there any good documentation (blog/forum post, etc.) that covers common techniques for handling the build-up of Mecanim root motion errors that cause agents to move off their mark over time?
There is a not uncommon use case where an agent needs to run various animations while staying on a particular mark (position/rotation) for an extended period of time. For example: A background character interacting with something or someone for the entire time the player is in the scene. Humanoid Mecanim can handle this fine if the agent has a single looping animation with zero root motion for the clip. But at soon as any transition between animations is introduced, even if the total delta motion for the clips is zero, small root motion errors will cause the agent to slowly, over minutes, drift off its position and rotation.
I've come up with several hacky solutions. But I'm hoping that someone has documented some good techniques for correcting the slow drift in position and rotation.
Answer by SteveFSP · Nov 28, 2016 at 04:59 PM
It has been a while with no answers to this question, so here is the solution I came up with.
The quick answer: Periodically use Animator.MatchTarget() on Root.
The trick is determining when to trigger the match operation. First, find all points in the sequence where the agent ends on a known position/rotation mark.
Of those, first try out the clips where there is a lot of translation and/or rotation at the end. This can be used to hide any corrections that occur during the match operation. E.g. As part of the sequence, an agent steps forward, does something, then steps back to its mark. The end of the step-back clip may have enough foot motion to hide corrections. You may need to experiment with the match duration to get the best result.
In cases where there is no suitable translation/rotation at the end of any of the clips, the next best option is to select a clip that has a lot of upper body activity. (E.g. The agent waving its hands around in an agitated manner.) The player will tend to focus on the upper body, making it less likely root corrections will be noticed.
After a location for the match operation has been selected, the next choice is how often to trigger the match. Sometimes the length of time between runs of the chosen clip is long enough to justify running a match every time the clip is run. Otherwise you can implement a timer that only allows the a match operation the next time the clip is run once the timer has triggered. Or you can choose something like "run the match every fifth time the clip is run".
There were several instances I've run into where I had to trigger a match operation every time a clip was run. This may have been fixed in more recent versions of Unity, but in some cases a large rotation periodically messed up the agent's rotation in a major way with just one run of the clip. In this case I always ran the match. E.g. I had a sequence of clips that caused the agent to turn around 180 degrees, do something, then turn back. Sometimes, with no detectable pattern, the agent would end the turn 20 to 40 degrees off from its expected rotation. That one required a match operation every time the clip was run, just in case the odd behavior occurred.
Hope this helps.
Answer by Teksel · Sep 12, 2019 at 03:29 PM
Hi! Thanks for the detailed answer. Take a look on this forum thread also. The Unity dev explaining the theory behind this - https://forum.unity.com/threads/apply-root-motion-rotating-object-not-accurate.439476/
My Solution to get rid of Root Motion error was: I have added couple extra frames in the beginning and the end of animation clip, so now engine almost never end up in position that the last important frame is cut-off because of the Update/Tick cycle. Worked for my case.
Your answer
Follow this Question
Related Questions
Smooth transition of position between animations 0 Answers
Root position or rotation are controlled by curves error on re activating game object! 1 Answer
how to fix roll animation resets to animation start position 0 Answers
Moving Character to exact position using animation. 0 Answers
Root Animations are inaccurate 0 Answers