Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by PippyLongbeard · May 22, 2014 at 12:35 AM · c#2danimatortransition

Check If Animator Animation Has Finished

Alright. So, I've started learning how to make 2D games. The script below is the basic script that controls the character's motions and animations. Everything works fine, except for kick. Because kick (indicated by the namesake comment) is a GetKeyDown instead of GetKey, the animation activates, then is immediately canceled by the idle checker (labeled 'Idle' below). I also can't do the same thing I did for the jump motion because I have no way to know when the kick animation has finished. So, my question is: how would I go about making sure that the kick animation has finished playing before it transitions to the idle animation. I hope that was a clear enough explanation. Thanks in advance for the help.

 public class Motion : MonoBehaviour {
     Animator anim;
     float moveVel;
     Vector2 dirVec = new Vector2(0,0);
     float jumpVel = 50.0f;
     bool jumping = false;
 
     void Start () {
         anim = transform.GetComponent<Animator> ();
     }
 
     void Update () {
         //Resets velocity to zero.
         dirVec = Vector2.zero;
 
         //Jump
         if(Input.GetKeyDown ("space")&&jumping==false)
         {
             transform.rigidbody2D.velocity = new Vector2(dirVec.x, jumpVel);
         }
 
         //Kick
         if(Input.GetKeyDown ("f"))
         {
             Debug.Log ("Kick.");
             anim.SetInteger ("AnimState", 2);
         }
 
         //Right
         else if(Input.GetKey ("d"))
         {
             anim.SetInteger("AnimState", 1);
             dirVec.x = 20.0f;
             transform.localScale = new Vector2(1, transform.localScale.y);
         }
 
         //Left
         else if(Input.GetKey ("a"))
         {
             anim.SetInteger("AnimState", 1);
             dirVec.x = -20.0f;
             transform.localScale = new Vector2(-1, transform.localScale.y);
         }
 
         //Idle
         else
         {
             anim.SetInteger ("AnimState", 0);
         }
 
         //Check if character is on ground. Resets jump counter.
         if(Physics2D.Raycast (new Vector2(transform.position.x + 1.15f, transform.position.y), -Vector2.up, 1.38f)||Physics.Raycast (new Vector2(transform.position.x - 1.15f, transform.position.y), -Vector2.up, 1.38f))
         {
             jumping = false;
         }
 
         else
         {
             jumping = true;
         }
 
         //Initialize the movement.
         transform.rigidbody2D.velocity = new Vector2 (dirVec.x, transform.rigidbody2D.velocity.y);
     }
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · May 22, 2014 at 12:51 AM 0
Share

Use Exit Time parameter in the state machine. Set the transition to near the end. Perhaps make it Atomic.

Animator has a StateInfo call to find which anim is playing on the referenced layer. I know its not the same but it helps.

avatar image PippyLongbeard · May 22, 2014 at 12:55 AM 0
Share

Oh, thank you sir! Didn't even know about that parameter. Fiddled around with it for a $$anonymous$$ute and got it working. You should probably post that as an answer, I'd accept it.

1 Reply

· Add your reply
  • Sort: 
avatar image
8

Answer by Tony-Sparrow · Sep 22, 2014 at 06:37 AM

if (this.animator.GetCurrentAnimatorStateInfo(0).IsName("YourAnimationName")) { // Avoid any reload. } you can search question before ask, it's here

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

How to make a transition animation, intermediate animation between two main states 0 Answers

Having some issues with my Animator Controller. 0 Answers

Instantiate Object position C#, not visible after new pos 1 Answer

how to controll second camera (MiniMap)? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges