LoginSignup
6
7

More than 5 years have passed since last update.

Animatorで現在再生中のステートの長さを取得する際の注意点

Posted at

Animatorから現在再生中のステートの長さを取得する際に少しハマったのでメモ

バージョン

Unity 2017.1.1f1

Animator.GetCurrentAnimatorStateInfo

GetCurrentAnimatorStateInfoで、現在のStateの情報(AnimatorStateInfo)を取得できます。
AnimatorStateInfoからはlengthが取得できますので、これで長さがわかります。
が、AnimatorStateInfoから取得できるlengthは、AnimatorのspeedやBlendTreeの評価が終わった後の値になっているようです。
どういうことかというと、Stateに1秒のAnimationClipが設定されている場合、
例えば、Animator.speedや、StateのSpeedが0.5になっていると、lengthは2になり、
両方に0.5ずつ設定されていると、4になります。
0になっていると、Infinityになります。

BlendTreeの場合は、ブレンド具合で変化します。
1秒と2秒のアニメーションを0.5でブレンドした場合、1.5といった具合になります。

各種speedを変更しない運用や、speedが反映されている情報が欲しい場合はこちらで問題ないと思いますが、
lengthを使って何か処理を行う場合は、Infinityがどうかチェックする必要がある場合があるかと思います。

Animator.GetCurrentAnimatorClipInfo

speed等が影響していない長さが欲しい場合は、GetCurrentAnimatorClipInfoでClipの情報(AnimatorClipInfo)を取得します。
AnimatorClipInfoからclipのlengthを参照すれば取得できます。
BlendTreeの場合、GetCurrentAnimatorClipInfoの第2引数にListを渡せば、現在ブレンドに使用されているClipの情報が取得できます。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7