Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Animation is playing but GetCurrentAnimatorStateInfo is giving false value.Why?

Discussion in 'Animation' started by zyonneo, Nov 8, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    282
    Already a default animation is playing.I am playing the animation by setting
    Code (CSharp):
    1. animmove.SetBool("Movement", true);
    by creating a second layer in animator to play two animations simultaneously.I added the code in start method the animation works at start .I commented the above code to check whether its working and it was not working.So i made sure because of the code the animation was working.


    When the animation stops I want to start another animation.

    Code (CSharp):
    1.  
    2. void Start()
    3.     {
    4.         animmove.SetBool("Movement", true);
    5.     }
    6. void Update()
    7.     {
    8. var aa = animmove.GetCurrentAnimatorStateInfo(0).IsName("Movement");
    9. Debug.Log("Animation Value = "+aa ); //When I give 0 parameter it giving me false and novalue when I test for 1
    10.     }
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    595
    According to the screenshot, your state is called "Move", not "Movement".
     
    zyonneo likes this.
  3. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    282
    animmove.GetCurrentAnimatorStateInfo(1).IsName("Move");
    need to change the parameter of GetCurrentAnimatorStateInfo also.
     
  4. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    282
    I changed to Move.You need to change the parameter also right?



    Code (CSharp):
    1.  IEnumerator IsAnimationdone()
    2.     {
    3.         while (animmove.GetCurrentAnimatorStateInfo(1).IsName("Move") && animmove.GetCurrentAnimatorStateInfo(1).normalizedTime < 1.0f)
    4.         {
    5.             //Wait every frame until animation has finished
    6.             yield return null;
    7.         }
    8.         Debug.Log("Animation has finished");
    9.         //Do something
    10.         speed = 10f;
    11.         float step = speed * Time.deltaTime;
    12.         animmove.SetBool("Movement", false);
    13.  
    14.         if (animmove.GetCurrentAnimatorStateInfo(1).IsName("Bite"))
    15.         {
    16.            
    17.             Showprefabs.transform.GetChild(0).GetComponent<Animator>().enabled = false;
    18.             Showprefabs.transform.GetChild(0).position = Vector3.MoveTowards(Showprefabs.transform.GetChild(0).position,
    19.                                                                             new Vector3(Arcam.transform.position.x, Arcam.transform.position.y + 0.2f,
    20.                                                                                         Arcam.transform.position.z + 6.3f), step);
    21.          
    22.         }
    23.     }
    Here the animation doesnt play.For some reason the Debug displays Animation has finished.If I remove the line animmove.SetBool("Movement", false); the animation play but cannot figure out when the animation has finished.Any idea what I am doing wrong here
     
  5. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    595
    Nothing is obviously jumping out at me as a likely reason for the animation to not play, but there are always plenty of possibilities for things to go wrong when using Animator Controllers and even at the best of times it's not entirely clear what the system is doing internally. I can't see your Console window, but that's always the first thing to check. But beyond that there isn't really any standard advice I can give you for debugging an Animator Controller because everyone seems to run into entirely different issues.

    If you get sick of wasting time investigating silly issues like that, you might be interested in Animancer (link in my signature) which lets you avoid Animator Controllers and just control everything with scripts. It has several easy ways of waiting for animations to finish built into it.
     
    zyonneo likes this.
  6. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    282
    I dont know what I did.I removed the IEnumerator and just gave the below code and it started working.I did not use this condition in if(animmove.GetCurrentAnimatorStateInfo(1).IsName("Move")).
    Code (CSharp):
    1.  
    2. if (animmove.GetCurrentAnimatorStateInfo(1).normalizedTime > 1)
    3.             {
    4.                
    5.                 animmove.SetBool("Movement", false);
    6.                 if (animmove.GetCurrentAnimatorStateInfo(1).IsName("Bite"))
    7.                 {
    8.                     Showprefabs.transform.GetChild(0).GetComponent<Animator>().enabled = false;
    9.                  
    10.                    //   Trying to update the position of prefab in front of the ARcamera(real close).But not happening.When the app starts it takes that position as camera position.How to update it.?          
    11.                     Showprefabs.transform.GetChild(0).position = Vector3.MoveTowards(Showprefabs.transform.GetChild(0).position,
    12.                                                                                     new Vector3(Arcam.transform.position.x, Arcam.transform.position.y ,
    13.                                                                                                 Arcam.transform.position.z +6.3f), step);
    14.  
    15.                    
    16.                 }
    17.  
    18.             }
    19.