Background
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 (https://docs.unity3d.com/ScriptReference/Animator.MatchTarget.html).
Problem
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 Animator.TargetPositon does the job:
// 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);
}
}
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 Animator.RootPosition (https://docs.unity3d.com/ScriptReference/Animator-rootPosition.html), but that just holds current character position.
Workaround
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".
Thank you