how to check animator state is finished unity3d
Solution 1
I figure it out, and I done it by checking state starts or not if starts then check for end, by states names. Below is code, and working fine, remember(in last state you have to create empty state)
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" );
}
}
}
}
Solution 2
You can use events on animation clips. It's explained in Unity manual: https://docs.unity3d.com/Manual/AnimationEventsOnImportedClips.html
In Animation Inport Settings in Annimations tab You can find Event heading. Position the playback to the end and click Add Event. Fill the Function field with name of the function to call at the end of animation. Just make sure that Game Object with this animation has a corresponding function.
Farhan Ali
Updated on June 14, 2022Comments
-
Farhan Ali almost 2 years
Below is my script, I want to check that animator state finished or not. If animator state(animation) is complete then do some action, but I am enable to do so, Thanks in advance.
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"); } } }