Search Unity

Question Finding humanoid scale factor

Discussion in 'Animation' started by Nathanieljla, Oct 5, 2022.

  1. Nathanieljla

    Nathanieljla

    Joined:
    Apr 18, 2014
    Posts:
    97
    Is there a method for querying the scale factor for a given humanoid (Or technique for calculating this)?

    For example, if I have a character that is 1m tall and one that is 3m tall and I play the same run animation on both, the run animation might report 4m/sec (from the import inspector), but at runtime the 3m tall character is actually moving at 12m/sec to compensate for the size difference. I need to apply this scale factor to some meta data that I generate for my characters, but I'm not sure how a humanoid size is determined.

    Thanks for the help!
     
  2. Nathanieljla

    Nathanieljla

    Joined:
    Apr 18, 2014
    Posts:
    97
    My solution was to extract the data from IAnimationJob, however this was my first dive into Playables, so if anyone knows an easier way to get the animationStream that's being used for the animator I'd love to know. This doesn't feel very "clean" IMO.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Playables;
    3. using UnityEngine.Animations;
    4. using System.Collections;
    5.  
    6. public struct AnimationJob : IAnimationJob
    7. {
    8.     public float humanScale;
    9.     public bool initialized;
    10.  
    11.     public void ProcessRootMotion(AnimationStream stream)
    12.     {
    13.     }
    14.  
    15.     public void ProcessAnimation(AnimationStream stream)
    16.     {
    17.         humanScale = stream.isHumanStream ? stream.AsHuman().humanScale : 1.0f;
    18.         initialized = true;
    19.     }
    20. }
    21.  
    22.  
    23. [RequireComponent(typeof(Animator))]
    24.  
    25. public class PlayAnimationSample : MonoBehaviour
    26.  
    27. {
    28.     PlayableGraph m_Graph;
    29.     AnimationScriptPlayable m_ScriptPlayable;
    30.  
    31.     private bool m_destroyGraph;
    32.     public float humanScale = 1f;
    33.  
    34.     void OnEnable()
    35.     {
    36.         // Create the graph.
    37.         m_Graph = PlayableGraph.Create("Human Scale Extractor");
    38.  
    39.         // Create the animation job and its playable.
    40.         var animationJob = new AnimationJob();
    41.         m_ScriptPlayable = AnimationScriptPlayable.Create(m_Graph, animationJob);
    42.  
    43.         // Create the output and link it to the playable.
    44.         var output = AnimationPlayableOutput.Create(m_Graph, "My Animator", GetComponent<Animator>());
    45.         output.SetSourcePlayable(m_ScriptPlayable);
    46.         m_destroyGraph = true;
    47.         StartCoroutine(Cleanup());
    48.     }
    49.  
    50.  
    51.     IEnumerator Cleanup()
    52.     {
    53.         while (!m_ScriptPlayable.GetJobData<AnimationJob>().initialized)
    54.         {
    55.             yield return null;
    56.         }
    57.  
    58.      
    59.         humanScale = m_ScriptPlayable.GetJobData<AnimationJob>().humanScale;
    60.         m_Graph.Destroy();
    61.         m_destroyGraph = false;
    62.     }
    63.  
    64.  
    65.     private void OnDisable()
    66.     {
    67.         if (m_destroyGraph)
    68.         {
    69.             m_Graph.Destroy();
    70.         }
    71.     }
    72. }
     
  3. Nathanieljla

    Nathanieljla

    Joined:
    Apr 18, 2014
    Posts:
    97
    Animator.humanScale. Wow. How did I miss this in my original doc search?