Search Unity

Checking if an animation is playing

Discussion in 'Scripting' started by Jsmucker, Nov 24, 2014.

  1. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    How would I check to see if an animation is playing? For instance I want to see if the animation "attack" is playing, then damage the player.
     
  2. federicopintaluba

    federicopintaluba

    Joined:
    Oct 19, 2014
    Posts:
    13
    I'm not sure about it, but let me suggest you using Animation Events in the Animation.

    In the Animation panel, at the last frame, or at the frame where the enemy attacks the player, create a new animation event:

    animationevent.png

    In this animation event, you can call to any function in the script attached to the gameobject where the Animator is attached.

    So, you can create a Script, or add a function to the existing one called "damagePlayer()", where you can damage the player.

    Hope this really helps.
     
  3. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    Thanks for the reply, I tried that before and couldn't seem to get it to work. Some issues with Defining a function. That is why I am trying this.
     
  4. federicopintaluba

    federicopintaluba

    Joined:
    Oct 19, 2014
    Posts:
    13
    Animation Events should work fine, whats exactly the problem you are having with it?
     
  5. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
  6. federicopintaluba

    federicopintaluba

    Joined:
    Oct 19, 2014
    Posts:
    13
    You create a function in a new script, you attach that script to the component that has de animation, at the animation editor you set that function at the final frame, as you can see in the image I attached earlier.
     
  7. Ricks

    Ricks

    Joined:
    Jun 17, 2010
    Posts:
    650
    If you want to check if an animation is playing then you can do:

    1) Get the animation component of the object
    2) if(animComp.IsPlaying("animationName"))

    This is legacy animations though, can't help with Mecanim. But if you don't know how to create a function you must learn programming first.
     
  8. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    It is a legacy animation, but that line of code doesn't seem to work. I'm trying this :
    Code (CSharp):
    1.     void OnTriggerEnter(Collider other) {
    2.                
    3.     if (animation.IsPlaying("attck01"))
    4.     {
    5.                                 SendMessage ("Damage", 1.0f, SendMessageOptions.DontRequireReceiver);
    6.                         }
    7.              
    8.         }
    It doesn't work. It does return an error: There is no animation attached to hero(which is the player gameobject). The script doesn't reference the hero and it is not on hero. Also, what do you mean by getting the animation component of an object?
     
    Last edited: Dec 3, 2014
  9. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Insert some Debug.Logs or step through the code in monodevelop's debugger... Does the object have a collider with isTrigger set attached to it?