Unity, C #. There is a model with animator, in which the state-animations and parameters for transitions between them are already configured.

I need to play animations in a certain order. To call the desired animation, I just check the desired parameter:

GetComponent.<Animator>().SetBool("someparameter",true); 

How to write the condition "when the current animation ends"? All the options that I find, or about Animation , not Animator , or too complex, and need a simple way in which you can quickly figure out and use.

    2 answers 2

    Animator has a GetCurrentAnimatorStateInfo method that receives information about the current state on the specified Animator Controller layer (AnimatorController). That is, it returns AnimatorStateInfo So they should be used.

    AnimatorStateInfo , in turn, has an IsName field - which says whether the name same as the active state in the statemachine . In the end, you might get something like this:

     private Animator animator; private void Start() { animator = GetComponentInChildren<Animator>(); } public bool IsAnimationPlaying(string animationName) { // Π±Π΅Ρ€Π΅ΠΌ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎ состоянии var animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0); // смотрим, Π΅ΡΡ‚ΡŒ Π»ΠΈ Π² Π½Π΅ΠΌ имя ΠΊΠ°ΠΊΠΎΠΉ-Ρ‚ΠΎ Π°Π½ΠΈΠΌΠ°Ρ†ΠΈΠΈ, Ρ‚ΠΎ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ true if (animatorStateInfo.IsName(animationName)) return true; return false; } 

    How to use:

     if (IsAnimationPlaying("Run")) Debug.Log("Player is running"); 

    To test several animations, you will most likely have to put them in an array and go over the loop:

     foreach (var move in attackMoves) { if (IsAnimationPlaying(move.animationName)) { // do smth... } } 

    PS GetCurrentAnimatorStateInfo - gets information on a specific layer. Therefore, to take the info on the most basic layer is GetCurrentAnimatorStateInfo(0) . In other layers, the index will change naturally.

    • Thank you so much? exactly what is needed! - Rumata

    It's not very clear what you want, if after one animation is over you want to switch to another, then set up the transitions between the states in Mecanim .

    If you need to catch the moment when the exit from the state is done, you can use the StateMachineBehaivour classes, which can react to the work in the state. This script is hung in Mecanim on the state itself. In the animator window, select the required state and for it in the inspector window add the appropriate script, as is done with the components on the scene.

    • Huge thanks, I will try this way too! - Rumata