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 RedHillbilly · Jun 25, 2014 at 01:37 PM · animatorcoroutine

Coroutines and animation triggers

Dear all,

I am currently working on a 2D game, and thought it would be a good idea to trigger the attack animations in coroutines, since I have several different attack types, and hence different times.

To do that I first launch the coroutine from the Update function when the attack-key is down:

 void Update (){
      if(Input.GetAxis("attack")>0.5f&&!hit)StartCoroutine("AttackManagement");
 }

Then in the coroutine, I first start the attack animation and set a bool to true (means I'm in the coroutine, I can't currently start a new one), then wait for some time (this is the part that waits for the animation to play), then launch the idle animation, again wait for some time, and finally set the bool to false (acts as cooldown) :

 IEnumerator AttackManagement(){
         hit=true;
         anim.SetTrigger("attack");
         Debug.Log("got here");

         yield return new WaitForSeconds(weaponTimes[equipedW]);

         Debug.Log("got here2");
         anim.SetTrigger("idle");
         Debug.Log("got here3");

         yield return new WaitForSeconds(weaponCoolDown[equipedW]);

         hit=false;
     }

And here comes the problem: the 3 debugging messages appear in the console, but the animation is not triggered. It either gets stuck in the "attack" state, or in the "idle" state, but does not behave the way I want.

In the animator I have different layers. Whats really strange, is that the trigger seems to work on some layers, but not on others.

Thank you a lot for your help :)

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 RedHillbilly · Jun 25, 2014 at 12:57 PM 0
Share

I did, and I am pretty sure I understand it correctly. I first implemented the same code as a finite state machine, and it worked, but with multiple Time.time calls, so I thought it would be easier to implement it as a coroutine. So my guess is the problem comes from my possible missuse of coroutines, not from the setTrigger function.

Here I am basically just using the 2 triggers as booleans. I could as well have used a bool and set it to true and then false.

Thanks for your answer anyway.

avatar image meat5000 ♦ · Jun 25, 2014 at 01:51 PM 0
Share

Indeed, path of least resistance is the best approach with Animator. I was indirectly checking you were referencing the name of a Parameter rather than a state or animation. Usually, for Parameters I give them names like "idleBool" or "walkFloat" to help me visualise the distinction when coding it.

To handle your stuck states, make sure there are appropriate transitions in your State $$anonymous$$achine (Animator window) leading from the Current to Go-To states as well as away from them. Decide which transitions require a bool and which can function from Exit Time.

avatar image RedHillbilly · Jun 26, 2014 at 08:52 AM 0
Share

Thanks for the nomenclature idea, I will definitely use that from now on.

For the second part: I think the states are implemented correctly. They do work in some layers, but the strange thing is that the parameter is not activating in all layers. Is it possible to deactivate a parameter for a given layer? $$anonymous$$aybe I did that by inadvertence. When I call setTrigger from the Update function the information is passed to all the layers, it's only in the coroutine that the information seems lost somwhere.

Thanks for your help so far :)

1 Reply

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

Answer by RedHillbilly · Jun 26, 2014 at 10:06 AM

I replaced the two trigger parameters by one boolean parameter that takes the states true and false, and it works now. I don't now why it wasn't working with the triggers, the mechanism should be the same. A bug related to coroutines maybe?

@meat5000 thanks a lot for your help, I should have tested that from the start, sorry that I wasted your time.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

coroutine cannot control physics of an object with animator 0 Answers

2D Animation does not start 1 Answer

Detecting last frame of animation, without StateMachineBehaviours or Animation Events 0 Answers

How to make a character move for some time and stand for some time? 1 Answer

Coroutine not working ? possible errors ? 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