title: Unity 3D animation.MatchTarget with root motion affecting only position or only rotation
body: <p><strong>Background</strong></p>
  
  <p>In the Unity 3D engine version 2017.3.1f1, I'm working on a 3D character driven by mecanim root motion. I need to rotate the character exactly 180° around the Y-axis (up) so it then walks left or right (it's 2D game). Because it's mecanim, angles are not always precise and character turns sometimes 177°, sometimes 181°. This is unwanted error causing the character to traverse in the Z-axis while walking. So I decided to correct the final angle with the built-in animator.MatchTarget() function (<a href="https://docs.unity3d.com/ScriptReference/Animator.MatchTarget.html" rel="nofollow noreferrer">https://docs.unity3d.com/ScriptReference/Animator.MatchTarget.html</a>).</p>
  
  <p><strong>Problem</strong></p>
  
  <p>Animator.MatchTarget doesn't have overloaded function to match only rotation so I need to enter the final position of the turn animation clip (the rotate animation has root motion and position change). I assumed member variable <em>Animator.TargetPositon</em> does the job:</p>
  
  <pre><code>// following script is attached to the character rotation Animator state
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class GenericState : StateMachineBehaviour {
  
       public float targetAngle_ = 180.0f;
  
       override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex){
  
           Quaternion targetRotation_ = Quaternion.Euler(0, targetAngle_, 0); 
           animator.MatchTarget(animator.targetPosition, 
                                targetRotation_, 
                                AvatarTarget.Root, 
                                new MatchTargetWeightMask(Vector3.one, 1f),
                                0f,
                                1f);
       }
  }
  </code></pre>
  
  <p>But it doesn't involve the root motion so the character ends at the exact position as it started the turn animation. There is another variable <em>Animator.RootPosition</em> (<a href="https://docs.unity3d.com/ScriptReference/Animator-rootPosition.html" rel="nofollow noreferrer">https://docs.unity3d.com/ScriptReference/Animator-rootPosition.html</a>), but that just holds current character position.</p>
  
  <p><strong>Workaround</strong></p>
  
  <p>The only solution I can think of is to read the animation data in the editor mode, store the root motion offset, then apply for each animation at runtime. This solution is overcomplicated and I'm looking for a simple alternative to "match just rotation and read target position from the root motion in the animation".</p>
  
  <p>Thank you</p>