Search Unity

Other Sample Animation several times per frame?

Discussion in 'Animation' started by Alex_Heizenrader, Dec 7, 2022.

  1. Alex_Heizenrader

    Alex_Heizenrader

    Joined:
    May 16, 2019
    Posts:
    95
    Hi,

    I am trying to sample an animation during the same game frame many times to decide which one is better for my interaction.

    I tried using Animator.Play, Motion Time controlled by a parameter and AnimationClip.SampleAnimation

    None of the above APIs seem to allow me to sample/scrub the animation to different times within the same frame. Is there something I am missing or a different way I could be trying to achieve the above?

    For reference I am just trying to do something like the following in Update:


    Code (CSharp):
    1. for (int i = 1; i < 1000; ++i)
    2. {
    3.    ...
    4.    animator.Play("CustomAnimation", 0, i/1000f); //for example
    5.    ...
    6. }
    Thanks
     
  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    127
    animator.Update(1/1000f);
    may proceed immediately (I think)
    If no, then try
    animator.playableGraph.Evaluate(1/1000f);
    . (you may want to cache playableGraph if you are doing it several times)
    Note that sampling animation many times in single frame has an impact on performance.
     
  3. Alex_Heizenrader

    Alex_Heizenrader

    Joined:
    May 16, 2019
    Posts:
    95
    I tried all those an none of them worked within the same frame, they all work but once at a time
     
  4. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    127
    Hello, The below sample code works for me.
    (the logs before and after the
    playableGraph.Evaluate(evaluateTimeStep)
    shows different results, and every loop has different results too)
    Hope this will help you.

    Code (CSharp):
    1. public class AnimatorMultipleEvaluateTestScript : MonoBehaviour
    2. {
    3.     // an animator moving itself back and forth
    4.     [SerializeField] private Animator animator;
    5.  
    6.     [SerializeField] private bool doEvaluate;
    7.     [SerializeField] private int evaluateSteps = 1;
    8.  
    9.     private static readonly float evaluateTimeStep = 1 / 60f;
    10.     private PlayableGraph playableGraph;
    11.  
    12.     void Start()
    13.     {
    14.         playableGraph = animator.playableGraph;
    15.         // don't auto update
    16.         playableGraph.SetTimeUpdateMode(DirectorUpdateMode.Manual);
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         if (doEvaluate) {
    22.             doEvaluate = false;
    23.             Evaluate();
    24.         }
    25.     }
    26.  
    27.     private void Evaluate()
    28.     {
    29.         for (var i = 0; i < evaluateSteps; i++) {
    30.             // Debug.Log(transform.position);
    31.             playableGraph.Evaluate(evaluateTimeStep);
    32.             // Debug.Log(transform.position);
    33.         }
    34.     }
    35. }
     
  5. Alex_Heizenrader

    Alex_Heizenrader

    Joined:
    May 16, 2019
    Posts:
    95
    Awesome, I will test this and report back, thank you! I never tested Evaluate at different intervals so maybe that was my missing point.