Starting an Animation based on it´s Length

I have an Enemy which is shooting at the player. Between every shot i have a reload time. For the Enemy i have two animations to sync with the shoots: The Shot itself with a little tail (recoild jitter) and an anticipation phase (a charge animation) which is telegraphing the shot.

The issue is to time those animations and stay flexible in terms of balancing the reload times and animation length/speed and allways have the animations match the ingame timings:

  • timing the shot is easy. when the reload time is over, i play the shot anim, trigger my particle effects and sound fx
  • The difficult part is the charge. no matter how long the animation clip is and to what playback speed the designers want to set it, i need to know it´s final length x to trigger it those x seconds before the reload timer expires

One should think that this info should be fairly easy to get, but i couldn´t figure it out yet.

AnimatorStateInfo hold all the Info you could need, but you can only get the current and the next:

animator.GetCurrentAnimatorStateInfo(0) gives your current state (layer 0) … too late for me.
animator.GetNextAnimatorStateInfo(0) gives you the upcoming state, but you need to be allready in that transition to the state … also too late

Apperantly there is no way to get a specific state out of an animator by id/index/name.

From the RuntimeAnimatorController you can readout all the Clips, look for a specific name and get its length. But this Lenght is ignoring any speed multipliers.

If anybody has found a way to get that info out of unitys classes or has another suggestion on how to achieve the desired behaviour, i´d be very thankful

Thanks for your answer. But the most important part, the speed setting on each state, is still missing. As i wrote i´m looking for a way to read out the speed setting of a specific state (without allready being in it or being in the transition to it)