- Home /
Coroutines and keypresses
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;
}
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.
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.
Almost is good enough if you're making Extreme Horseshoes.
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.
Your answer
Follow this Question
Related Questions
Can't get while loop to execute more than once in a coroutine before yielding [Solved] 2 Answers
coroutine not running after yield new WaitForSeconds 1 Answer
Coroutine Countdown Timer being extremely slow 1 Answer
Destroy all instantiated prefabs in Scene when Coroutine stops 1 Answer
What key was pressed? 4 Answers