2017-05-09 74 views
0

下面是我的脚本,我想检查一下动画师状态是否完成。如果动画制作者状态(动画)完成,那么请执行一些操作,但我可以这样做,谢谢。如何检查动画师状态完成unity3d

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

@RiaanWalters这就是动画。 OP正在使用Animator。他们都不同。 – Programmer

+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

您可以使用动画片段上的事件。它的团结说明书中的说明: https://docs.unity3d.com/Manual/AnimationEventsOnImportedClips.html

动画运行轨迹设置Annimations选项卡您可以找到事件标题。将播放定位到最后,然后单击添加事件。填充函数字段,其​​中包含要在动画结束时调用的函数的名称。只要确保具有此动画的游戏对象具有相应的功能即可。