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;
	}

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.

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.