Animator.MatchTarget () from point a to point b.

I’m trying to implement a controller for an NPC that follows a grid like path using root motion.

At the moment, my script is constantly checking if the character has reached its destination and/or requires rotation, but the inaccuracies (even with tolerances) are causing me a lot of grief.

From what I’ve ready so far, it looks like MatchTarget is the solution to this problem. Are there any good tutorials that make use of MatchTarget for things like climbing, jumping, and path-following when the exact destination is known?

I’m fairly certain I can figure out turning and basic walking with a bit more experimentation, but implementing climbing and jumping (when the exact destination is known) seems like a daunting task with only a few minor examples to work from.

Lastly, the targetNormalizedTime parameter is confusing since I have no way of knowing how many times the animation must loop in order to reach a particular destination.

Thanks

After a bit more experimentation it doesn’t appear as if you can use MatchTarget to move a figure to a specific location over time. I thought I could try something like this in FixedUpdate, but it just jumps to the location.

...
const float walkspeed= 1.477f;
float distance = Vector3.Distance(new Vector3(0, 0, 10), transform.position);
float clipDuration = 1.067f;
float metersPerClip = clipDuration * walkspeed;
float loopsRequired = distance * metersPerClip;

anim.MatchTarget(new Vector3(0, 0, 10), Quaternion.identity, AvatarTarget.Root, new MatchTargetWeightMask(new Vector3(1, 1, 1), 0), 0, loopsRequired);
...

Any ideas?

According to the documentation “values greater than 1 can be set to trigger a match after a certain number of loops”

Is this a bug?

I tried this myself. Indeed, after loopsRequired my animation just jump to last frame, motion root is not applied as well.

I don’t think MatchTarget is best for the case you are describing.

Have you tried LERP (for position) and SLERP (for rotation)? Unfortunately you will have to give up the root motion to control your position precisely. You can just turn off root motion while your figure is moving to the specific location using LERP and then turn off the LERP and turn on the root motion again once the location is reached.

Since I made this post, I gave up on trying to use root motion for walking and running. I’m now using physics and curves to mimic human acceleration / deceleration. Of course, it looks like my character is sliding all the time, which I hate.

Recently, I did manage to get root motion working for climbing.

The trick was to uncheck “Bake Into Pose” on the animation and manually update the position and rotation within OnAnimatorMove().

public void OnAnimatorMove()
{
        figure.transform.position = animator.rootPosition;
        figure.transform.rotation =  animator.rootRotation;
}

I might give this a try for walking and running and see what happens.