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 conguerror · Aug 19, 2020 at 09:24 AM · animationcoroutine

Coroutine is not working

Animation should be played every few seconds but it does not

 if (Vector3.Distance(transform.position, enemy.position) <= throwRadius)
                 {
                     if (!threw)
                     {
                         animator.SetTrigger("Throw");
                         threw = true;
                     }
                     else
                     {
                         StartCoroutine(Wait());                   
                     }
                 }
             }
     IEnumerator Wait()
         {
             yield return new WaitForSeconds(5f);
             threw = false;
         }

animation is still playing without delaying it for a 5 seconds, just one after another.Code is in update func

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 Llama_w_2Ls · Aug 19, 2020 at 09:52 AM 0
Share

Well im not sure exactly what the default state of threw is, is it true or false. If it starts at false, then it sets the trigger 'Throw' to true and then sets threw to false. Thats it. It doesnt run the else. If it starts at true, then it will wait for 5 seconds (nothing is happening in this time), then it will set threw to false. Thats it. Im not sure if this code is in an update() or in a loop, but if not, your animation wont play with a delay. You need to rework the logic of your code, but I cant do it for you because I dont know the full context of the script. Hope you figure it out @conguerror

avatar image conguerror Llama_w_2Ls · Aug 19, 2020 at 12:43 PM 0
Share

@Llama_w_2Ls Thanks for response. I will try out your suggestion right away. and yes, code is in Update function, I wrote it in question

avatar image conguerror Llama_w_2Ls · Aug 19, 2020 at 12:57 PM 0
Share

@Llama_w_2Ls So it delayed it only once. Afterwards same thing happened, animation was triggered one after another(I also turned off self transition). I put coroutine in while loop as well, it did not help. I want animation to be triggered once in few seconds. I have read forum post about it, did not find any solution.

2 Replies

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

Answer by wooolly · Aug 19, 2020 at 01:16 PM

I'd rework this to remove the coroutine entirely and use a simple countdown field in your class instead. lastThrown would be the countdown field and should start off at 0f (float).

 if (lastThrown > 0f) { 
     lastThrown -= 1f * Time.deltaTime; 
 }
 else if (within distance) {  
     trigger;
     lastThrown = 5f; // preferably replace '5f' with a CONSTANT
 }

The snippet above will do the same thing as your coroutine, yet is simpler. I don't know the overhead that coroutines come with, but I'd think this is certainly optimal too. I only use coroutines in specific circumstances.

p.s. if your animation is still replaying instantly, it's likely you've configured the animation incorrectly. Double check to make sure the animation isn't set to loop, and test playing the animation just once to start with (adding in some temporary code to play it once in the Start method or something) - make sure the trigger correctly leaves the animation state and returns to another state.

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 conguerror · Aug 19, 2020 at 01:22 PM 1
Share

Damn! thanks, I will check it out now . I don't know why I did not think of it -__-.

avatar image wooolly conguerror · Aug 19, 2020 at 01:29 PM 1
Share

Haha yeah sometimes it's easy to get sidetracked with one solution that other solutions don't come to $$anonymous$$d. I hope it works out alright.

avatar image conguerror wooolly · Aug 19, 2020 at 01:36 PM 1
Share

It did. Thanks man. Indeed other solutions were out of my $$anonymous$$d

avatar image
1

Answer by Aviryx · Aug 19, 2020 at 01:30 PM

A coroutine is typically it's own function that can be invoked - not something that is directly placed inside of Update().


 // increase someInteger by 1 every five seconds.

 public int someInteger;
 private bool isWaiting;

 void Update()
 {
     if (!isWaiting)
     {
         StartCoroutine(someCoroutine());
     }
 }

 private IEnumerator someCoroutine()
 {
     isWaiting = true;
     yield return new WaitForSeconds(5);
     someInteger++;
     isWaiting = false;
 }
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

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

Coroutine mystery (for a noob) 1 Answer

Can I make animations snap to a frame? 1 Answer

Animation Event issue what am I doing wrong here 2 Answers

Running coroutines in a recursive function. How to wait until all coroutines done before going to next level 1 Answer

Best practice for programmatic character animation 2 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