Forum Discussion
gabriead
12 years agoHonored Guest
Help: How to detect user input in nested Coroutines?
Hi guys, I am having an issue with detecting when a key was pressed inside some nested Coroutines.
What my code does so far is, detecting pairs of GameObjects, then according to the Objects play a specific soundfile. In addition it should then measure the time that passed from when the sound file stopped playing to when the user first hit the "y" or "m" key.The second Coroutine(measuring the time) should wait until the user has pushed either the "y" key or the "m" key and should not be allowed to proceed before an input was made.
Therefore I have 2 nested Coroutines (SelectSound->handelAudio) It's getting pretty frustrating as I can not figure out what the Problem is. Here is the Code:
I have tried to use Input.GetKey but that did not work inside the coroutine. Also
e.type == EventType.KeyDown && e.keyCode == KeyCode.Y did not work. In both cases the while loop did not work and unity crashed.
I also tried to use update() and OnGUI() in combination with the second Coroutine "handleAudio" but that crashed as well.
I am thankful for any tip on how to solves that issue.
What my code does so far is, detecting pairs of GameObjects, then according to the Objects play a specific soundfile. In addition it should then measure the time that passed from when the sound file stopped playing to when the user first hit the "y" or "m" key.The second Coroutine(measuring the time) should wait until the user has pushed either the "y" key or the "m" key and should not be allowed to proceed before an input was made.
Therefore I have 2 nested Coroutines (SelectSound->handelAudio) It's getting pretty frustrating as I can not figure out what the Problem is. Here is the Code:
I have tried to use Input.GetKey but that did not work inside the coroutine. Also
e.type == EventType.KeyDown && e.keyCode == KeyCode.Y did not work. In both cases the while loop did not work and unity crashed.
I also tried to use update() and OnGUI() in combination with the second Coroutine "handleAudio" but that crashed as well.
I am thankful for any tip on how to solves that issue.
//Choosing sound according to the gameObject
IEnumerator selectSound(GameObject object1, GameObject object2)
{
this.nameObject1 = object1.name;
this.nameObject2 = object2.name;
if (nameObject1 == "Bottle" && nameObject2 == "Book") {
audio.clip=audioBottleBook;
yield return StartCoroutine(handleAudio());
}
if (nameObject1=="Bottle" && nameObject2=="Cup")
{
audio.clip=audioBottleCup;
yield return StartCoroutine(handleAudio());
}
if (nameObject1=="Bottle" && nameObject2=="Lamp")
{
audio.clip=audioBottleLamp;
yield return StartCoroutine(handleAudio());
}
Debug.Log ("End select sound");
}
IEnumerator handleAudio()
{
audio.Play ();
yield return new WaitForSeconds(audio.clip.length);
//Save point in time when audio has finished
this.t_0=Time.realtimeSinceStartup;
this.keyPushed = false;
while (keyPushed==false)
{
Event e = Event.current;
if ((e.type == EventType.KeyDown && e.keyCode == KeyCode.Y))
{
Debug.Log ("Current Key pushed :" + this.currentKeyPushed);
keyPushed=true;
}
if ((e.type == EventType.KeyDown && e.keyCode == KeyCode.M))
{
Debug.Log ("Current Key pushed :" + this.currentKeyPushed);
keyPushed=true;
}
yield return null;
}
1 Reply
Replies have been turned off for this discussion
- ryahataHonored GuestAny particular reason why you are doing this with coroutines?
I think that a simple finite state machine would work here. Here's how I would approach the problem.- Keep track of a few states (Idle, PlayingSound, WaitingForInput)
- In the Idle state you would look for your pairs of objects. When a pair of objects is found you can play the associated file.
- In the PlayindSound state you would wait for the sound to finish playing and move the WaitingForInput state when playback has completed. There are two ways you could do this.
- Check to see if AudioSource is playing (every Update)
- Start a Coroutine that fires when the sound clip is done.
- In the WaitingForInput state you would look for your key presses. When a valid one is pressed go back to Idle.
You should be able to implement this in the Update() function alone (provided you don't use a coroutine to check if the sound file has finished playing)
Hope this helps!
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 3 years ago
- 2 years agoAnonymous
- 2 years ago