Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Percentage through animation in state (or percentage through state)

Discussion in 'Animation' started by Newcomma, Jul 12, 2016.

  1. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Hi There,

    I want to know what percentage through an animation (or the current clip) I am in a state

    Code (CSharp):
    1.     // OnStateMove is called right after Animator.OnAnimatorMove(). Code that processes and affects root motion should be implemented here
    2.     override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    3. // Check percentage through anim clip here and do something based on that
    4.     }
    I can't easily find a way to do this from the docs, I suspect I'm missing something trivial as this seems like a fairly obvious operation?

    Thanks in advance,

    Matt
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    You can store the entry time in OnStateEnter, and find the length of the current state with the stateInfo's .length parameter.

    So something like:

    Code (csharp):
    1. private float entryTime;
    2.  
    3. public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    4.     entryTime = Time.time;
    5. }
    6.  
    7. public override void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    8.     float currentPercentage = (Time.time - entryTime) / stateInfo.length;
    9. }
     
    Cyber-Dog likes this.
  3. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Bingo675, K3IRRR and musap_ like this.