• 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
10
Question by Paulo-Henrique025 · Nov 20, 2012 at 11:00 PM · animationmecanim

How to get current state on Mecanim?

I am using this tutorial as guide http://youtu.be/4L8RWPZV-v8 but it calls AnimatorStateInfo.name which seems to have been removed from the class.

I want to call my "Atak2" if Atak_Key is pressed during "Atak1":

PseudoCode:

 if(currentInfo.name == "Atak1" && Input.GetKey("Atak_Key"))
 {
      //Set bool on mecanim to call animation Atak2
 }

How to do it now?

Comment
Add comment · Show 1
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 obi_juan_ · Oct 31, 2016 at 03:17 PM 0
Share

I'm sure there has to be a reason to store animations states and clips in hash tables, but I don't understand what's the problem having a method like GetCurrentAnimatorStateInfo(0).name. Why I have to get first the hash of the name? This push us to realize before the name of the state. Things like this makes everything complicated for an easy and fast access to the animator behavior.

2 Replies

· Add your reply
  • Sort: 
avatar image
16
Best Answer

Answer by jwinn · Nov 25, 2012 at 05:38 AM

Check out the latest Mecanim tutorial download, which has changed code since the beta. I was just dealing with integrating all the mecanim animator changes today.

Here's an example of how they set it up. This is assuming your base layer is named "Base":

 static int atakState = Animator.StringToHash("Base.Atak1");    
 
 void Start (){
     anim = GetComponent<Animator>();                      
 }
 
 void FixedUpdate ()
 {
     currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
 
     if (currentBaseState.nameHash == atakState){
          Debug.Log("Do Stuff Here");
     }
 }
Comment
Add comment · Show 2 · 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
avatar image Paulo-Henrique025 · Feb 06, 2013 at 07:43 PM 0
Share

I ended doing something like that, but my attack combo logic never got perfect. I''ll need quite some time to adapt to $$anonymous$$ecanim. I'm having serious issues on checking the time of the animation to set if the player can call the second combo. Anyway, thanks!

avatar image senad · Feb 26, 2013 at 07:40 PM 0
Share

I think you are not supposed to use timings anymore, but define events inside the animation system. (but I only read this somewhere as I am too just starting to use mecanim)

avatar image
41

Answer by RChrispy · Jan 08, 2014 at 02:09 PM

How About using something like this:

     if( anim_Animator.GetCurrentAnimatorStateInfo(0).IsName("MyAnimationName"))
     {
      //Do something if this particular state is palying
     }

This Function returns true when the string is the same as teh StateName you play currently. Else false.

Comment
Add comment · Show 8 · 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
avatar image Chandni12345 · Oct 09, 2015 at 12:11 PM 1
Share

thank you so much

avatar image MindnightMeep · Jan 18, 2016 at 01:38 AM 2
Share

Is it possible to simply get the name of the current state as a string so I can print it out? I'd rather not have to check against each state name in the controller to find the right one.

avatar image Yip37 MindnightMeep · Feb 04, 2016 at 02:17 AM 1
Share

I was thinking the same thing. I want to know if the animation playing contains "attack", because I have a lot of attack animations. Now I have a condition inside the if for every attack ¬¬ .

Edit: I just realized it can be done with tags. So I would just tag every attack animation with "Attack", so if it matches that tag, that's it. Don't know if that solves your problem. Thinking out loud just in case. gl.

avatar image J0hn4n Yip37 · Jan 28, 2017 at 09:08 PM 0
Share

Oh my god thank you brother, i have 2 days searching info about mecanim, I was using $$anonymous$$achineStates its very messy the thing, actually this thing its usefull to logic things, i will tag my anims :D

avatar image splitter20 · Feb 11, 2016 at 03:13 AM 1
Share

This just saved me a huge headache! Thank you for the post.

avatar image RedDead7693 · May 05, 2016 at 05:40 AM 1
Share

This is such a great example of a question that often leads to other developers scolding beginners like me before finally reverting with paragraphs of code. Thank you so much for this easy to understand answer, it works perfectly!

avatar image RChrispy RedDead7693 · May 05, 2016 at 09:44 AM 1
Share

Glad I could help so many people! :)

avatar image Howgyn · Nov 29, 2016 at 01:38 PM 0
Share

@Dev6_RC Can you please fix my code? This is the link where I attach my problem http://gamedev.stackexchange.com/questions/133689/getcurrentanimatorstateinfo0 I'm getting red line at GetCurrentAnimatorStateInfo and I don't know how to solve it.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Why my animation isn't working with Mecanim 0 Answers

How do you import keyframe animations from Maya into Unity? 1 Answer

How to switch between procedural animation to Mecanim / Animator animations 0 Answers

How to animate model with some mesh missing? 2 Answers

Import animations dynamically from asset bundle : Mecanim 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges