Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How do i check if an animation is playing?

Discussion in 'Animation' started by TheOrself3301, May 2, 2024.

  1. TheOrself3301


    Nov 16, 2021
    Howdy, i'm trying to make a recoil animation for a weapon in my game and i need to animation to start immediately upon firing. right now it waits for the animation to finish before it loops again. here's what i have:
    Code (CSharp):
    1. if (GunModel.GetComponent<Animator>().IsPlaying("PistolRecoil") == true)
    2.         {
    3.             GunModel.GetComponent<Animator>().Rewind("PistolRecoil");
    4.         }
    5.         else
    6.         {
    7.             GunModel.GetComponent<Animator>().Play("PistolRecoil");
    8.         }
    Novice programmer for me is an overstatement but looking at the unity documentation i don't see why this should be working? im getting a compiler error that says "The animator does not contain a definition for 'IsPlaying' (or 'Rewind')" although the documentation says otherwise? Any help is greatly appreciated
  2. ArachnidAnimal


    Mar 3, 2015
    Where does it say Animator has an IsPlaying and Rewind function?
    It is the Animation that has IsPlaying, not Animator. You're confusing the two.

    FYI: To check if an Animator is in some state named "PistolRecoil", presumablly playing a PistolRecoil animation:
    Code (csharp):
    2. var isPlayingAnimation = animator.GetCurrentAnimatorStateInfo (0).IsName ("PistolRecoil");