How to know end coordinates of animation?

I want to know coordinates of root bone in the end of animation. I tried to put unit with Animator controller programmaticaly to the scene, play animation and execute Update() several times. But coordinates are slightly different from the real. Is there method to know accurate coordinates in the end of animation?

This code should work as it should, but it doesnt work accurately:

       animator.transform.position = new Vector3(0, 0, 0);
       animator.transform.rotation = Quaternion.Euler(0, 0, 0);          
       animator.Play(_hashName, 0, 0);
       AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
       duration = stateInfo.length;
       float t = 0;
       float delta = 0.01f;
       while (t < duration)
       {
           animator.Update(delta);
           t += delta;
       }
       Vector3 endCoords = animator.transform.position;

For example, this method returns end coords like (5.33; -2.09; 0) instead (5.32; -2.11; 0) in a game scene.
How can I know exact end coordinates?

There is a special method for this

In your case you want to get the of

there is a catch, you need to manually call Update to evaluate the target
then you can use

animator.SetTarget(AvatarTarget.Root, 1.0f);
animator.Update(0);
Vector3 position = animator.targetPosition;
1 Like

I forgot to add that you need to be in the right state before calling this method

animator.Play(_hashName, 0, 0);

1 Like

Thanks a lot for the answer! Everything works perfectly now.
P.S. I read documentation, but I was misled by the phrase “The position is only valid when a frame has being evaluated after the SetTarget call”. Of course SetTarget() called in Update() method.

If you want to result immediatly you need to manually call and animator.Update(0)
because if you only do

targetPosition won’t be updated correctly

1 Like

There’s no helpful information at that doc page, so I googled the method hoping someone somewhere might explain it, and all I found was a different forum thread where someone asks for an explanation and gets pointed back to this thread. Might be an idea to get someone to flesh out the docs for this feature.

1 Like