L.Th
L.Th

Reputation: 103

Unity - Change character only when animation stopped

i have two objects(the same character, but, in different functions) which i want change the character when animation stop and when it runs triggered by a click. For example, I have the Kick_Player where there is the animation triggered by the click, and when the Kick_Player ends the animation, i want it automatically changes to Player_Stopped. The poses are different each other, 'cause this i need to do these changes.

I tried something with this.MyAnimator.GetCurrentAnimatorStateInfo(0).IsName("My_Animation") but, i got unsuccessful tries. Is there a way to do that ?

public class TapController : MonoBehaviour {

    Animator Anim;

    public GameObject CharacterToController; //Kick_Player
    public GameObject CharacterToBeStopped; //Player_Stopped

     void Start(){
      Anim = CharacterToController.GetComponent<Animator>();
      CharacterToBeStopped.SetActive(false);
     }

     void Update(){
      if(input.GetMouseButtonDown(0)){
       if(!CharacterToController.activeSelf){
        CharacterToController.SetActive(true);
       }

       Anim.Play("Kick_Ball");


     if(!this.Anim.GetCurrentAnimatorStateInfo(0).IsName("Kick_Ball") {
        CharacterToController.SetActive(false);
        CharacterToBeStopped.SetActive(true);
      }
     }

}

I made this code to test, but it doesn't work

Upvotes: 0

Views: 299

Answers (1)

Programmer
Programmer

Reputation: 125275

Using the IsName function requires that you prefix the base layer name of the animation state before the actual animation state.

The default base name is usually "Base Layer"

enter image description here

if(!this.Anim.GetCurrentAnimatorStateInfo(0).IsName("Base Layer.Kick_Ball") 

Note that you have to do that outside your if(input.GetMouseButtonDown(0)){ otherwise that will never get chance to be checked.


I've seen reports of IsName not working for some people so if you do that but still have issues, consider doing it another way.

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        if (!CharacterToController.activeSelf)
        {
            CharacterToController.SetActive(true);
        }

        Anim.Play("Kick_Ball");

        StartCoroutine(PlayAndWaitForAnim(Anim, "Kick_Ball"));
    }
}


const string animBaseLayer = "Base Layer";
int animHash = Animator.StringToHash(animBaseLayer + ".Kick_Ball");

public IEnumerator PlayAndWaitForAnim(Animator targetAnim, string stateName)
{
    targetAnim.Play(stateName);


    //Wait until we enter the current state
    while (targetAnim.GetCurrentAnimatorStateInfo(0).fullPathHash != animHash)
    {
        yield return null;
    }

    float counter = 0;
    float waitTime = targetAnim.GetCurrentAnimatorStateInfo(0).length;

    //Now, Wait until the current state is done playing
    while (counter < (waitTime))
    {
        counter += Time.deltaTime;
        yield return null;
    }

    //Done playing. Do something below!
    Debug.Log("Done Playing");

    CharacterToController.SetActive(false);
    CharacterToBeStopped.SetActive(true);
}

Upvotes: 1

Related Questions