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
6
Question by xandermacleod · Jun 24, 2013 at 07:10 AM · animationmecanimtransitiononce

Triggering an animation once in mecanim

Hi there,

Within one of my animators I am tracking whether or not to transition into another state using a bool and the SetBool() function.

However I seem to have the problem of if I turn the bool back to being false mid animation, the transition goes back to my idle too quickly and doesn't play the animation all the way through, even if the conditions-to-transition-back-to-idle are set to ExitTime&bool=false.

Can anyone tell me what I need to do to set the bool back to being false at the right time / how I can get mecanim to play an animation once?


If possible as well I'd also like to have the ability to interrupt the animation and get it to transition to its beginning (in my case 'shoot a gun animation with a little recoil afterwards getting interrupted by firing the gun again').

Comment
Add comment · Show 4
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 infinitypbr · Jun 24, 2013 at 07:14 AM 0
Share

I'm very new w/ $$anonymous$$ecanim, but I didn't think you could have multiple transition conditions?

I would set the condition to start animation be bool = true, and the condition to end animation be exit time, so that it goes back to idle as soon as it's done playing your animation, and it doesn't matter that bool only was true for one frame.

avatar image xandermacleod · Jun 24, 2013 at 07:19 AM 0
Share

you click the plus button in the transition window to get a new AND logic-gate transition. The problem there is that the moment you set it the bool to being false again the animation goes back too early, it doesn't wait until the end of the anim. And if you don't set the bool to being false again the character will never play idle again, it'll keep seeing the bool is true and jump right back into the other animation.

avatar image infinitypbr · Jun 24, 2013 at 08:31 AM 0
Share

Can you set the bool to false via code? That's what I've been doing. Basically I said, "if bool = true && timePast > 0.5" set bool = false. The time is arbitrary, but long enough to be more than one frame, but not longer than the animation. As long as the transition out of the animation isn't when bool=false, it should be fine I think.

avatar image lucaep · Dec 16, 2016 at 09:24 AM 0
Share

$$anonymous$$ake sure that on the state in mecanim/animator tab that Has Exit Time is checked.

4 Replies

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

Answer by TonyLi · Jun 24, 2013 at 01:28 PM

The example scripts provided by Unity use GetCurrentAnimatorStateInfo(). In Update(), it checks if you're in the "shoot gun" state. If so, it clears the bool. Check the bazooka script in the IK scene (http://u3d.as/content/unity-technologies/mecanim-example-scenes/3Bs).

It sounds like you've maybe marked the shoot-->idle transition as non-Atomic. Try ticking the Atomic checkbox. Also, is the exit time correct?

Otherwise, I'd guess it's in the logic of how/when you're setting the bool. You should be able to clear the bool as soon as you're in the shoot state (actually, as soon as the transition to shoot has started).

If you clear it at this point, then you have two choices for interrupting the animation and starting it over right away:

  1. Create a transition from Any State-->shoot, or

  2. Create a null state. Create a transition from shoot-->null (if bool is true) and then null--> shoot (again if bool is true).

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 rhys_vdw · Aug 15, 2013 at 05:40 AM 0
Share

It's a shame there isn't a a nicer interface for forcing transitions...

avatar image
7

Answer by s_guy · Nov 20, 2013 at 01:44 AM

The easiest way is to use the "Trigger" parameter type for the transition in the Animator state editor. It's a bool that sets itself to false immediately after the transition.

I was using animation events and scripted the bool reset before I came across this.

Comment
Add comment · Show 3 · 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 infinitypbr · Nov 20, 2013 at 01:48 AM 0
Share

Yeah, this is new for Unity 4.3, so not something that wasn't available until this past week.

The trigger feature is $$anonymous$$ILLER.

avatar image s_guy · Nov 20, 2013 at 01:57 AM 0
Share

Lucky me that I found it by accident. I don't think it's even documented yet.

Would you $$anonymous$$d taking a look at this animation question I had?

http://answers.unity3d.com/questions/579213/how-do-you-hold-an-animation-on-the-last-frame-wit.html

avatar image Pavel_D · Feb 02, 2014 at 07:07 PM 0
Share

This trigger very help (i think about anim events) but i found this post and... my two pistol system easy simple on mecanim i have blend tree (PistolState(idle/run)) and than two transitions PistolL and PistolR(and some name trigger)

 if(WeaponState.IsName("Weapon.Pistol-shotR")){ secondShot = true;}
 if(WeaponState.IsName("Weapon.Pistol-shotL")){ secondShot = false;}
 // if one pistol only this
 if(Input.GetButtonDown("Fire") && secondShot && WeaponState.IsName("Pistol.PistolState"))    {
 animator.SetTrigger("PistolShotR");
 Shoot(PistolR);
 }
 if(Input.GetButtonDown("Fire") && !secondShot && WeaponState.IsName("Pistol.PistolState"))    {
 animator.SetTrigger("PistolShotR");
 Shoot(PistolL);
 }

"The trigger feature is $$anonymous$$ILLER." I am agree)

P.S. But 1 update you must configure Transitions to PistolShot state had time to bummed 1 times

avatar image
5

Answer by beit · Jul 04, 2015 at 12:38 PM

A bit from here a bit from there I get it solved, here is the how to:

  • Create an animation by dragging X sprites onto the scene

  • Open the animation controller that is created (you can find it on the sprite asset folder)

  • Create a trigger shouldAnimate (on top left you find parameters, click on +)

  • Create an empty state

  • Set the default transition to the empty state

  • From the new state add a transition to the animation state

  • Under the transition condition add the shouldAnimate trigger

  • Create an empty transition from the animation to the new state

  • From code call myAnimator.setTrigger("shouldAnimate")

  • ENJOY :D

Cheers

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 animalphase · May 13, 2016 at 02:11 AM 0
Share

Thank you! I have been searching all over the internet, but this is the first simple explanation this I have found!

avatar image Yacuzo · Dec 16, 2016 at 09:04 AM 0
Share

Great! Weird that it was so hard to find a simple description on how to wire up the trigger. I was missing the empty transition and things got all buggy.

avatar image
1

Answer by m4a44 · Sep 21, 2013 at 06:25 PM

Click on your transition and set it to "Solo". That should force the animation to play fully before it goes back (even if you set the bool to false before the full playthrough).

Comment
Add comment · Show 4 · 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 rhys_vdw · Sep 22, 2013 at 03:14 AM 0
Share

Good point. But you still need a reliable way to set the bool to false after the transition.

avatar image m4a44 · Sep 22, 2013 at 03:17 AM 0
Share

I've just set them false in a late update. Not the best, but it works... Hopefully Unity will add a feature to modify parameters soon...

avatar image rhys_vdw · Sep 22, 2013 at 03:20 AM 0
Share

Nice, but what if you're moving from a state that is also in solo and not yet able to transition? The flag would be reset before the state changes.

avatar image m4a44 · Sep 22, 2013 at 03:23 AM 0
Share

Have a check in the late update to see if you are in the desired state? I haven't needed to play around with that yet...

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

How to stop mesh disappearing during Animator state transition blend time? 1 Answer

Mecanim transition condition can't have both bool and trigger? 0 Answers

Mecanim state problem 1 Answer

What are those white curves in the animation transition panel - And can I read it in script? 0 Answers

Mecanim Blend Tree: transition not smooth 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