關於unity Animator 怎么判斷一個動畫播放結束這里有幾種方法。希望對大家有幫助。還有其他辦法的可以分享一下
第一種方法:在動畫結束幀后面加個動畫事件,調用下含這個變量的函數接口不是可以了?
如圖,找到動畫的inspector面板,在里面有個Events下拉條,下拉后在想要的幀的位置添加事件函數,函數名字記得在使用這個動畫的物體的腳本里面寫好,否則會報錯
第二種方法:試試animator上面那個 exit time
第三種方法:
//獲取動畫層 0 指Base Layer.
AnimatorStateInfo stateinfo = animator.GetCurrentAnimatorStateInfo(0);
//如果正在播放walk動畫.
if(stateinfo.IsName("Base Layer.walk"))
{
}
問:請問一下動畫狀態機怎么判斷動畫是否播完了?
答:
1. 腳本參考
AnimatorStateInfo.normalizedTime
float normalizedTime;
Description:
Normalized time of the State.
The integer part is the number of time a state has been looped. The fractional part is the % (0-1) of progress in the current loop.
2. 代碼如下:
1 private Animator animator; 2 void Start() 3 { 4 animator = this.GetComponent<Animator>(); 5 } 6 7 void Update() 8 { 9 AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(0); 10 // 判斷動畫是否播放完成 11 if (info.normalizedTime >= 1.0f) 12 { 13 DoSomething(); 14 } 15 }
- if (anim.IsPlaying("roar") && anim["roar"].normalizedTime >= 1)
bool IsAnimationPlaying(GameObject objWithAnimation,string animationName) { return objWithAnimation.animation.IsPlaying(animationName)&&objWithAnimation.animation[animationName].normalizedTime<<span>1.0f; } }
判斷某個動畫是否播放完畢。
IEnumerator WaitForAnimationPlayOver(GameObject objWithAnimation,string animationName) { yield return new WaitForSeconds(objWithAnimation.animation[animationName].length); } }
等待某個動畫播放完成。
normalizedTime: 范圍0 -- 1, 0是動作開始,1是動作結束