Hi guys, some questions relating to coroutines and keypresses. So my code is supposed to do the following:
- Play an animation clip from a series of pre-created clips
- Wait for a key press when the animation clip ends
- 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;
}