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
0
Question by AntLewis · Feb 08, 2012 at 11:06 AM · coroutineskeypress

Coroutines and keypresses

Hi guys, some questions relating to coroutines and keypresses. So my code is supposed to do the following:

  1. Play an animation clip from a series of pre-created clips

  2. Wait for a key press when the animation clip ends

  3. Repeat for the remaining clips

Now the playing seems to work ok, but I'm having trouble with waiting for the keypress. I would imagine that while I'm not pressing a key, the Debug.Log ( Waiting for keypress") output would be displayed every frame. It displays it twice, then just stops. Any advice on this, as ever, would be much appreicated.

 private void OnEnable ()
     {
 
         StartCoroutine ( testFunction () );
     }
     
     private IEnumerator testFunction()
     {
           foreach (AnimationClip clip in animationClipsInSequence) {
             yield return StartCoroutine (PlayAnimation (clip.name));
         }
     }
     
     private IEnumerator PlayAnimation (string animationClipToPlay)
     {
 
         referenceToAnimation.Play (animationClipToPlay);
         
         while (referenceToAnimation.IsPlaying ( animationClipToPlay ) )
         {
             Debug.Log("Playing animation clip "+animationClipToPlay);
             yield return new WaitForSeconds(1.0f);
         }
         Debug.Log ("Animation stopped playing");
          
         yield return StartCoroutine ( WaitForKeyPress() );
         yield return 0;
 
     }
  
     
     private IEnumerator WaitForKeyPress ()
     {

         while (!Input.anyKey) {
             Debug.Log ( Waiting for keypress");
             yield return new WaitForEndOfFrame();
         }
          
         yield return 0;
     }
Comment
Add comment · Show 1
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 AntLewis · Feb 08, 2012 at 11:59 AM 0
Share

It doesn't seem right to have input on a coroutine...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tingham · Feb 08, 2012 at 01:18 PM

We've dealt with several issues that function inconsistently inside of enumerating methods with regards to timing, etc. You should use Update to detect your system events and set booleans based on the results from the update loop. If you go this route you won't have any trouble with detection inside of your coroutines (because you'll be evaluating local variables instead of picking out of the event queue.) This will also have the added benefit of making your code a bit more readable.

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 Owen-Reynolds · Feb 08, 2012 at 03:54 PM 0
Share

The manual really does say that Input.xyz stuff is only correct in Update. But, also took me forever to notice that. The problem is it almost works outside.

avatar image tingham · Feb 08, 2012 at 04:32 PM 0
Share

Almost is good enough if you're making Extreme Horseshoes.

avatar image
0

Answer by Czpal · Jul 10, 2021 at 03:59 PM

I came across something similar. Although this is a very old topic it might help someone, so here's the code I came up with.

CoroutineUtilitiesInput.cs

 public class CoroutineUtilitiesInput : MonoBehaviour
 {
     private bool _anyKeyDown = false;
 
     public bool GetKeyDownAndReset()
     {
         if (_anyKeyDown)
         {
             _anyKeyDown = false;
             return true;
         }
 
         return false;
     }
 
     void Update()
     {
         if (Input.anyKeyDown)
         {
             _anyKeyDown = true;
         }
     }
 }

and now using this class

 public static IEnumerator WaitForAnyKeyDown(Transform target)
 {
     CoroutineUtilitiesInput cui = target.gameObject.AddComponent<CoroutineUtilitiesInput>();
     yield return new WaitUntil(cui.GetKeyDownAndReset);
     UnityEngine.Object.Destroy(cui);
 }

Although if you are calling this a lot you might want to reuse CoroutineUtilitiesInput, not destroy it afterwards like I did.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What key was pressed? 4 Answers

Create GUIText by pressing key 1 Answer

Can't get simple script to work? 4 Answers

how can I destroying a spawn object ? 1 Answer

turning audio on and off 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