下面是我的脚本,我想检查动画师状态是否结束。如果动画师状态(动画)完成然后做一些动作,但我可以这样做,提前谢谢。
using UnityEngine;
using System.Collections;
public class fun_for_level_complet : MonoBehaviour
{
public Animator animator_obj;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
check_end_state ();
}
public void level_complete()
{
if (this.GetComponent<movement_of_player> () != null)
{
this.GetComponent<movement_of_player> ().enabled = false;
}
animator_obj.SetBool ("congo",true);
}
public void check_end_state ()
{
// here I want to check if animation ends then print
// my state name is congo
// animation name Waving
// using base layer
if (animator_obj.GetCurrentAnimatorStateInfo (0).IsName ("congo") && !animator_obj.IsInTransition (0))
{
Debug.Log ("anim_done");
}
}
}
答案 0 :(得分:1)
我弄清楚了,我是通过检查状态启动与否来完成的,如果启动然后通过状态名称检查结束。下面是代码,工作正常,请记住(在最后状态,你必须创建空状态)
using UnityEngine;
using System.Collections;
public class fun_for_level_complet : MonoBehaviour
{
public Animator animator_obj;
private string[] states = new string[]{ "congo" };
private string current_state_name = "";
private bool waiting_end_state = false;
private bool wait_for_anim_start = false;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (waiting_end_state)
{
if (wait_for_anim_start)
{
if (animator_obj.GetCurrentAnimatorStateInfo (0).IsName (current_state_name))
{
wait_for_anim_start = false;
}
} else
{
check_end_state ();
}
}
}
public void level_complete()
{
if (this.GetComponent<movement_of_player> () != null)
{
this.GetComponent<movement_of_player> ().enabled = false;
}
animator_obj.SetBool ("congo",true);
waiting_end_state = true;
wait_for_anim_start = true;
current_state_name = states [0];
}
public void check_end_state()
{
if (!animator_obj.GetCurrentAnimatorStateInfo (0).IsName (current_state_name))
{
waiting_end_state = false;
if( current_state_name==states[0] )
{
GameObject.FindGameObjectWithTag ("inagmegui").SendMessage ("make_it_true");
print ( "animation has been ended" );
}
}
}
}
答案 1 :(得分:1)
您可以在动画片段上使用事件。它在Unity手册中解释: https://docs.unity3d.com/Manual/AnimationEventsOnImportedClips.html
Annimations 标签中的动画输入设置您可以找到事件标题。将播放结束到最后,然后点击添加活动。在功能字段中填写要在动画结束时调用的函数的名称。只需确保具有此动画的游戏对象具有相应的功能。
答案 2 :(得分:0)
如果您没有任何过渡,并且希望动画在第0层的“ stateName”结束时得到通知,则可以通过调用以下IEnumerator来实现:
public IEnumerator PlayAndWaitForAnim(Animator targetAnim, string stateName)
{
targetAnim.Play(stateName);
//Wait until we enter the current state
while (!targetAnim.GetCurrentAnimatorStateInfo(0).IsName(stateName))
{
yield return null;
}
//Now, Wait until the current state is done playing
while ((targetAnim.GetCurrentAnimatorStateInfo(0).normalizedTime) % 1 < 0.99f)
{
yield return null;
}
//Done playing. Do something below!
EndStepEvent();
}
主要逻辑是一旦进入状态,我们应该检查“ normalizedTime”变量的小数部分是否达到1,这表示动画已达到其结束状态。
希望这会有所帮助