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
0
Question by Levantez · Oct 16, 2013 at 02:30 PM · mecanimstringname

How do I retrieve Mecanim State Name?

Hi, what happen is that I have this repetitive code here which should return true whenever the Animation is in a State with the name "Platform" in it. And the thing is, I cannot figure out how to get Animator state name and retrieve it as a string.

Can anyone help me on this?

 protected bool CompareBaseState(string stateName)
     {
         AnimatorStateInfo currentState = anim.GetCurrentAnimatorStateInfo((int)MOTIONLAYER.BASE);
         
         if(currentState.nameHash == Animator.StringToHash(stateName)) { return true; }
         return false;
     }
 
 //Mecanim States that are putted into this, will not response to move input
     protected bool IsInUncontrollableState()
     {          
         if(CompareBaseState("Base.Platform_Cross"))
         {
             return true;
         }
         
         if(CompareBaseState("Base.Platform_StepUp"))
         {
             return true;
         }
         
         if(CompareBaseState("Base.Platform_MoveUp"))
         {
             return true;
         }
         
         if(CompareBaseState("Base.Platform_RunUp"))
         {
             return true;
         }
         
         return false;
     }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by infinitypbr · Oct 17, 2013 at 07:59 PM

Maybe I'm reading your question wrong, but this is what I do:

in Start()

 stateIdDie         = charAnimator.StringToHash("Base Layer.Die");  //repeat for all states

Whenever I start a new animation, like "attack 1" or "magic 1" etc, I set my lastAnimation variable to the state I've chosen, and then lastHash to match the hash of that animation

 function SetHash()
 {
     if (lastAnimation == "die")
             lastHash = stateIdDie;
 }

And in Update()

 if(charAnimator.IsInTransition(0) && charAnimator.GetNextAnimatorStateInfo(0).nameHash == lastHash)
         charAnimator.SetBool(lastAnimation, false);

Basically this says if the Animator is not in transition (and is therefor, in a single animation), and the next state is the same as the lastHash I've set (as in, when this animation is done, it will then repeat to the same animation -- like looping an attack animation etc -- then set the bool to false, to keep the animator from looping.

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
avatar image
0

Answer by vikitosss · May 18, 2016 at 04:47 AM

 var anim = GetComponent(Animator);
 
 // with or without -OnAnimatorMove function -
 function OnAnimatorMove(){
         
     if( anim.GetCurrentAnimatorStateInfo(0).IsName("WALK")) {
         print( "Current state is WALK" );
     }
     else if( anim.GetCurrentAnimatorStateInfo(0).IsName("JUMP")) {
         print( "Current state is JUMP" );
     }
     else if( anim.GetCurrentAnimatorStateInfo(0).IsName("....")) {
         print( "Current state is..." );
     }
 }

or if you like it :
if( !anim.GetCurrentAnimatorStateInfo(0).IsName("WALK")) { anim.SetBool("WALK", true); }

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

16 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

Related Questions

How to get around hard-coded string in Unity? 1 Answer

transform.name value to a variable 2 Answers

Does Game objects names affect anything in build ?,does Game objects names affect anything? 1 Answer

Converting String to Variable Name 3 Answers

How to manage these animations and avatars? 0 Answers


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