Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Check if gameobject is playing specific animation

Discussion in 'Scripting' started by Littlenorwegian, Feb 18, 2016.

  1. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    There's two pages because IsPlaying(...) and isPlaying are different (see first letter). Not to mention first is method while second is property. What issues are you having? It doesn't correctly tell you it's playing?
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Make sure you're looking at the right class. The Animation component is legacy - you're probably looking for the Animator class. As far as I'm aware, the Animator class does not have a function that does what you're looking for directly; however you could use State Machine Behaviours attached to the animation in question to do it.
     
  4. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    Woah, this got complicated fast.

    Well, to put it in blunt terms, I'm not able to have this script check if Player is currently playing the animation DigUp.
    The script I posted, does not work. And I'm now more than a little confused what something being legacy or what state machine behaviors or how that plays into this.
     
  5. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    Where did you post the script? I can't see it.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    LEGACY:
    When Unity first came out, animations were done using the component named Animation. For a while, this was good and straightforward, but in Unity 4 they introduced a new system, marketed as "Mecanim" and using the component called Animator.

    They left Animation in because people would have imported projects and plugins and assets which used the old system, and it'd be rude to just break all that functionality. So instead, it was marked as "Legacy", and for newly imported objects, you have to sort of go out of your way to use Animation; everything defaults to Mecanim/Animator. So odds are, your character has imported with an Animator component, and if you're using the class functions from the Animation class instead, You're Gonna Have a Bad Time (TM).

    STATE MACHINE BEHAVIOURS:
    Mecanim is based on what's called a Finite State Machine - a set of "states", each of which may, under certain conditions, lead to a different state. You've probably seen the editor window, or at the very least screenshots, that looks like this. Each of those boxes represents a state (and each contains one AnimationClip).

    Unity recently added a feature whereby scripts could be attached to the State itself, and receive message when the Animator enters or exits that state (as described in the link I posted above). For your problem, you would have a StateMachineBehaviour that would set a particular variable to true on enter, and to false on exit; if that variable is true, your character is playing that animation.
     
  7. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Why not just keep some sort of variable around and change that when you play your animation, then just check the var and see what is the animation that is playing.
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    I'm guessing the animation plays for a bit, then when the animation finishes it returns to its default state. His code knows when it enters that animation, but it'll have no idea when it ends. Hence the StateMachineBehaviour.
     
    RavenOfCode likes this.
  9. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    OK, so, much to my embarrassment, I am still not able to do this.

    Here's the situation.
    Player enters the trigger for Mound, which is a prefab.
    Mound has a bool attached to it, which if active makes the Player DigDown.
    Then teleports to another area, then plays DigUp.

    Problem here is simply that while the script works, it kinda breaks if you spam the dig button.
    Because there is another Mound gameobject on the other side that reacts to Player entering its trigger so you can effectively break the animation system by warping back and forth.

    TL;DR
    I am still stuck trying to figure out a system where all the script needs to do is check if Player is currently playing the DigUp animation.
    Googling around has just confused me as there's been some changes so older posts seem to just not work.
    And I can't really wrap my head around StateMachineBehaviors, StarManta.
    I'd hate to ask, but it would help with an example of how it would work in this case.
     
  10. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    assuming you're sticking with the legacy then...

    from the OP, this is malformed
    Code (csharp):
    1.  
    2. If (Player.!anim.IsPlaying("DigUp"))
    3.  
    ! signifys "not", you can't include that where you have

    Code (csharp):
    1.  
    2. If (!Player.anim.IsPlaying("DigUp"))
    3.  
    this is where the ! goes, assuming "Player" is a reference to something, that reference has a public "anim" property that is an "animation" component and you want to check that that animation component is playing the "DigUp" animation by name.


    I'd still recommend that you learn the newer animator system, probably starting with the "controlling animation" tutorials in the learn section
    https://unity3d.com/learn/tutorials/topics/animation
     
  11. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    Well, hot damn. I got it working. :)
    And big thanks for linking me that, Lefty Righty.

    I will certainly try having experienced this.
    Thanks, guys. Really.