Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to know end coordinates of animation?

Discussion in 'Animation' started by ivolkov, Feb 15, 2017.

  1. ivolkov

    ivolkov

    Joined:
    Feb 15, 2017
    Posts:
    7
    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:
    Code (CSharp):
    1.        animator.transform.position = new Vector3(0, 0, 0);
    2.        animator.transform.rotation = Quaternion.Euler(0, 0, 0);          
    3.        animator.Play(_hashName, 0, 0);
    4.        AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
    5.        duration = stateInfo.length;
    6.        float t = 0;
    7.        float delta = 0.01f;
    8.        while (t < duration)
    9.        {
    10.            animator.Update(delta);
    11.            t += delta;
    12.        }
    13.        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?
     
  2. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    There is a special method for this
    https://docs.unity3d.com/ScriptReference/Animator.SetTarget.html

    In your case you want to get the of
    https://docs.unity3d.com/ScriptReference/AvatarTarget.Root.html

    there is a catch, you need to manually call Update to evaluate the target
    then you can use
    https://docs.unity3d.com/ScriptReference/Animator-targetPosition.html
    https://docs.unity3d.com/ScriptReference/Animator-targetRotation.html

    Code (CSharp):
    1.  
    2. animator.SetTarget(AvatarTarget.Root, 1.0f);
    3. animator.Update(0);
    4. Vector3 position = animator.targetPosition;
    5.  
     
    ivolkov likes this.
  3. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    I forgot to add that you need to be in the right state before calling this method

    animator.Play(_hashName, 0, 0);
     
    ivolkov likes this.
  4. ivolkov

    ivolkov

    Joined:
    Feb 15, 2017
    Posts:
    7
    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.
     
  5. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    If you want to result immediatly you need to manually call and animator.Update(0)
    because if you only do
    1. animator.SetTarget(AvatarTarget.Root, 1.0f);
    2. Vector3 position = animator.targetPosition;
    targetPosition won't be updated correctly
     
    closingiris likes this.
  6. bfoddy

    bfoddy

    Joined:
    Mar 27, 2012
    Posts:
    85
    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.
     
    noisetree likes this.
  7. noisetree

    noisetree

    Joined:
    Apr 10, 2015
    Posts:
    51
    Hello! Wanted to ask if SetTarget correctly works with humanoid retargeting, i made test and in "basic" model it works fine, but with retargeted character the position is not correct comparing to animation (basic and retargeted have same bone structure and rigged similary, though have difference in proportions). Or maybe what should i know to make it work with retargeting correctly?
     
    Last edited: Jul 4, 2023