Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
11
Question by Ross_S · Oct 02, 2014 at 03:58 PM · animationtriggermecanimboolean

Mecanim Trigger getting stuck in 'TRUE' state

So, I have some triggers in the Animation Controller - these work fine most of the time... I call SetTrigger() from the code and they trigger once and then are immediately false - waiting to be triggered again. But occasionally i notice that the anim is auto-happening when it should be waiting for the trigger. When i check in the Animation Controller I can see a little tick next to the trigger indicating it's constantly turned on... behaving like a 'bool' - ... this seems to be a Unity bug... anybody got any ideas ? Thanks

Comment
Add comment · Show 7
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 Jesse_Pixelsmith · Oct 11, 2014 at 04:37 AM 0
Share

I'm seeing this as well for my melee attack cycle, definitely seems like a Unity bug, nothing that I'm doing seems like it should cause this.

avatar image Sylker Jesse_Pixelsmith · Apr 06, 2015 at 07:57 AM 0
Share

Yeah, it seems to be a bug. I am stuck with that too and using bool ins$$anonymous$$d. Gotta set it to false all the time =/

avatar image Mochnant Jesse_Pixelsmith · Jun 23, 2015 at 08:21 PM 0
Share

I've also encountered this bug in 5.1.1f. I was not able to find an open ticket in the issue tracker on it.

avatar image LordDarkon76 Jesse_Pixelsmith · Dec 15, 2015 at 04:56 PM 0
Share

It isnt a bug,

The trigger is an auto managed bool, if you set a trigger it will stay true until a transition is triggered.

avatar image bitstrider LordDarkon76 · Feb 24, 2016 at 07:19 AM 0
Share

not just any transition either...it must be one that uses the trigger as a condition

avatar image meat5000 ♦ · Nov 19, 2014 at 01:38 PM 0
Share

This probably comes down to just a few things.

Ti$$anonymous$$g! Polling states can often result in a mismatch of ti$$anonymous$$g meaning the animation is looped or fired as the off condition is not set in time or has been totally missed.

Not Clearing a Set item. http://docs.unity3d.com/ScriptReference/Animator.ResetTrigger.html

Exit Time parameter - This is notorious for messing up even the simplest of Anim chain ti$$anonymous$$gs.

avatar image Cherubyxx · Feb 13, 2015 at 04:30 PM 0
Share

I've also had a similar experience although the ResetTrigger did not help unless I put it into a check for the next state.

 if (Animator.GetCurrentStateInfo(0).nameHash = _defaultState)
     {
        Animator.ResetTrigger("AnimationYouWantToReset");
     }

9 Replies

· Add your reply
  • Sort: 
avatar image
33

Answer by georgeq · May 31, 2015 at 06:02 PM

Ultroman's answer is not 100% exact. The problem does not (always) occur when you trigger an animation that is currently being played. It does occur when you trigger an amination while a transition is still running.

Let's say you have 2 animations: A and B and 2 triggers: tA and tB, animation A plays when tA gets triggered and animation B plays when tB gets triggered. If you trigger tA animation A will start playing, but if you trigger tB before animation A has finished, tA won't be reset, it will remain true until animation A is played from begining to end. The same also happens if you trigger tA for a second time while animation A is still playing.

So, if for some reason you need to trigger tB and you are not sure if animation A has finished, you should use ResetTrigger, I didn't understand why the guys at Unity decided to include this funtion until I stumbled upon this problem.

To prevent this unwanted behaviour you should do something like this:

 animator.ResetTrigger("tA");
 animator.SetTigger("tB);
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
5

Answer by Ultroman · Nov 19, 2014 at 12:44 PM

2021 update: Check out the answer above instead or follow this link: http://answers.unity.com/answers/977718/view.html

ORIGINAL ANSWER: I found out that there is a problem with Mechanim, if you try to trigger the same animation that is already running. I don't think it is built for that. It is a state-machine after all, so it makes sense that it should never trigger an animation that is already running.

You "simply" have to check if the animation you're starting is already running.

I've done these steps to ease my life:

  1. Made one trigger for each of my animations, with the same name as the animation I want to trigger! (this is important, if you want to use the simple functions below)

  2. Made a transition from "Any State" to each of my animations, and the only condition for each transition, is the matching trigger.

  3. Then I made a PlayAnimation function, which checks if the animation is already running:

.

 public void PlayAnimation(string animationName){
     if (!_animator.GetCurrentAnimatorStateInfo(0).IsName(animationName))
         _animator.SetTrigger(animationName);
 }

...and here's one where you can control the layer you're checking:

 public void PlayAnimation(string animationName, int layer){
     if (!_animator.GetCurrentAnimatorStateInfo(layer).IsName(animationName))
         _animator.SetTrigger(animationName);
 }

The IsName() function checks if the string you pass it, is the name of the currently playing animation. If it isn't, I use the trigger with the same name. Simple.

This works for me.

The animations trigger instantly, though.

I don't know if this interrupts non-atomic animations.

EDIT: Since doing this, I somehow got my Animator working normally, so now I can just do:

 _animator.Play(animationName);

It doesn't screw up anymore, and I don't need to use triggers at all. And also, I can do this to restart the running animation from scratch, even if it is the same animation:

 _animator.Play(animationName, layer, 0.0f);

I don't know why, but this wasn't working yesterday. _animator.Play() just didn't do anything.

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 Ultroman · Nov 19, 2014 at 02:46 PM 0
Share

Thanks to meat5000 for pointing out a glaring error in my last edit.

avatar image xiaoken · Dec 25, 2014 at 02:48 PM 0
Share

Thank you for the good answer. You can make it more flexible by using static classes to hold variable information so you could end up with code something like:

if(_currAnimatorBaseState.nameHash != $$anonymous$$yAnimatorState.Idle){ _animObj3D.SetTrigger(AnimatorConditionNames.$$anonymous$$akeIdle);}

where you have, for example, $$anonymous$$yAnimatorState being

public static class $$anonymous$$yAnimatorState { public static int Idle= Animator.StringToHash("Base Layer.Idle"); }

Just my two cents.

avatar image
3

Answer by tjPark · Dec 15, 2015 at 09:02 AM

This is how I did in my game after reading the articles.

 string currentAnimation = null;

 void setAnimation(string stringInput){
     Animator anim =transform.GetComponent<Animator>();
     Debug.Log("Setting Anim to : " + stringInput);
     if(currentAnimation==stringInput){

     }else{
         if(currentAnimation!=null){
             anim.ResetTrigger(currentAnimation);
         }
         anim.SetTrigger(stringInput);
         currentAnimation=stringInput;
     }
 }
Comment
Add comment · Show 1 · 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 l3fty · Mar 15, 2016 at 11:20 PM 0
Share

This is a tidy snippet that solved the issue I was having, thanks!

avatar image
3

Answer by Aily · Jun 05, 2016 at 09:04 AM

you need to turn on flag "can transition to self" in transition settings rollout.

Comment
Add comment · Show 1 · 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 michaelday008 · Sep 19, 2021 at 08:31 PM 0
Share

I wish I could give you more than 1 vote. I discovered your comment after it took me 2 hours to figure out that "can transition to self" was not checked!

avatar image
1

Answer by guy.moreillon · Dec 02, 2015 at 03:41 PM

For what it's worth, I've had this problem when setting the trigger while in a state that had no exit condition based on that trigger. The trigger was simply not consumed.

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
  • 1
  • 2
  • ›

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

44 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 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

Mecanim Trigger Stays too long true 1 Answer

Totally new noob to mecanim and unity itself - Trigger animation? 0 Answers

Trigger Activated Multiple Times From One Click 2 Answers

How can I change a mecanim animation by pressing a key? 1 Answer

Mecanim Boolean 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