• 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
2
Question by MOrlando616 · Mar 31, 2015 at 08:58 PM · mecanimanimationstate

MecAnim SubState Animation Complete

Hi folks,

Browsing around online, I've seen a lot of questions and answers revolved around determining if a specific animation is playing or not. What I need to do is check if any animation in a particular sub-state is playing.

Instead of storing the hash for every animation in a sub-tree (and having to add code each time a new animation is added), I'd rather simply make a check to see if any animation inside my sub-state is playing or not.

Is that feasible at all?

Comment
Add comment · Show 3
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 digzelot · Mar 31, 2015 at 10:22 PM 0
Share
avatar image MOrlando616 · Apr 01, 2015 at 12:09 AM 0
Share
avatar image digzelot · Apr 01, 2015 at 12:32 AM 0
Share

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Ash-Blue · Jun 08, 2015 at 01:47 PM

Attempting t$$anonymous$$s in Unity 4.6 and below will result in a huge mess of unmaintainable code (made that mistake a w$$anonymous$$le back). But with Unity 5's s$$anonymous$$ny new State Mac$$anonymous$$ne Behaviours you can easily monitor a sub-state with a few lines of code. In order to do t$$anonymous$$s we'll need to monitor a bool parameter on our Animator component.

Add a new bool to the animator representing when you are in your sub-state such as "attacking." T$$anonymous$$s worked best for me since I'm monitoring the end of my attack animation sub-state.

On your desired sub-state mac$$anonymous$$ne, add t$$anonymous$$s script by selecting it and clicking "Add Behaviour".

 public class SubStateMonitor : StateMac$$anonymous$$neBehaviour {
    [SerializeField] string myBool;

     override public void OnStateEnter(Animator anim, AnimatorStateInfo stateInfo, int layerIndex) {
         anim.SetBool(myBool, true);
     }
 
     override public void OnStateExit(Animator anim, AnimatorStateInfo stateInfo, int layerIndex) {
         anim.SetBool(myBool, false);
     }
 }

You can now check if the sub-state mac$$anonymous$$ne is running anywhere with the following code snippet.

 Animator anim = GetComponent<Animator>();
 bool isSubState = anim.GetBool(myBool);

For more info on State Mac$$anonymous$$ne Behaviors please see the following resources.

  • https://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-mac$$anonymous$$ne-behaviours

  • http://docs.unity3d.com/ScriptReference/StateMac$$anonymous$$neBehaviour.html

  • http://blogs.unity3d.com/2014/06/26/s$$anonymous$$ny-new-animation-features-in-unity-5-0/

Comment
Add comment · Show 6 · 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 josencv · Nov 23, 2015 at 02:12 AM 0
Share
avatar image Ash-Blue josencv · Nov 23, 2015 at 05:00 AM 0
Share
avatar image TheWarper · Aug 02, 2017 at 12:27 AM 0
Share
avatar image Ash-Blue TheWarper · Aug 03, 2017 at 03:08 PM 0
Share
avatar image RickshawDerpyDerp · Jan 04, 2020 at 12:31 PM 0
Share
avatar image Ash-Blue RickshawDerpyDerp · Jan 04, 2020 at 07:44 PM 0
Share
avatar image
0

Answer by RickshawDerpyDerp · Jan 04, 2020 at 07:35 AM

SOLUTION!!!

The above author might have made a typo. We do not want to overwrite OnStateEnter() or OnStateExit(); we want to overwrite OnStateMac$$anonymous$$neEnter() and OnStateMac$$anonymous$$neExit(). Put t$$anonymous$$s in your new script and remember to define your boolean in the Animator state mac$$anonymous$$ne, and enter the name in the serialized field in the editor:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SubStateMonitor : StateMac$$anonymous$$neBehaviour
 {
     [SerializeField] string myBool;
     override public void OnStateMac$$anonymous$$neEnter(Animator animator, int stateMac$$anonymous$$nePathHash)
     {
         animator.SetBool(myBool, true);
     }
 
     override public void OnStateMac$$anonymous$$neExit(Animator animator, int stateMac$$anonymous$$nePathHash)
     {
         animator.SetBool(myBool, false);
     }
 }

You can do the same boolean check as before (`animator.GetBool("YourBooleanNameHereInQuotes")`);

ORIGINAL POST

So, I can test when I'm inside a state mac$$anonymous$$ne now but it doesn't seem to recognize when I am inside a state anymore with (for example)

if (animator.GetCurrentAnimatorStateInfo(0).IsName("First_Punch"))

inside the state mac$$anonymous$$ne "Attack_Punch"

EDIT I have confirmed that when I am inside an animation state INSIDE a submac$$anonymous$$ne, that inside the new behaviour script we wrote for the submac$$anonymous$$ne, we automatically go through OnStateExit(), w$$anonymous$$ch turns our new boolean to false. T$$anonymous$$s may be because I am already checking to see if I am inside another animation state (see IF statement above) and Unity can't allow me to be in two animation states at once (or check for it, or perhaps it has somet$$anonymous$$ng to do with indexing). During the transition between animations, the I can see that OnStateEnter() is ran again and the new boolean's value is true again.

So t$$anonymous$$s is either an unfortunate oversight or (preferably) I am forgetting to do somet$$anonymous$$ng when I run my existing code inside an IF statement that does if (animator.GetBool("sm_Attack_Punch")){ //Existing code, if statements here}.

Can somebody please clarify?

alt text alt text

In the above state mac$$anonymous$$ne, if I attach the behaviour to Battle_JumpAttack, check that I am inside that state mac$$anonymous$$ne, and provided that is true, check that I am inside the first state called Jump_Start_Beginning, the current state will not be Battle_JumpAttack anymore when I am running the first animation.


annotation-2020-01-03-223901b.png (66.1 kB)
annotation-2020-01-03-223901.png (68.8 kB)
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Animator Transition between layers ? 0 Answers

Animation state not transitioning at 100% 1 Answer

Javascript controlling Mecanim? 1 Answer

How to Determine What AnimationClip/AnimationState Generated an AnimationEvent? 0 Answers

How can I tell what AnimationState mecanim Animator is in from script? 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