How would I have a script either wait on a timer OR for a button press?

Hello, I am currently writing an RPG talk script. Concept is simple enough, you talk to a character, and I’m essentially trying to code in an option to skip their dialogue. Here’s a snippit of code:
IEnumerator Wait(){ yield return new WaitForSeconds (4.0f); FirstResponses (); }

So, as you can see, its currently set to continue on after 4 seconds, which works perfectly fine. I’ve tried a few methods to no avail to try to add a keycode check to see if the player presses a key, but I can’t seem to get it right. I want it to continue the timer in case the player doesn’t skip the text, but an option that would allow them too otherwise.

Try this:

 IEnumerator Wait(){
        bool continueDialog = false;
        float startTime = Time.time;

        while(!continueDialog){
            if(Input.GetKeyDown(KeyCode.Space)){
                continueDialog = true;
            }
            else if(Time.time - startTime >= 4f){
                continueDialog = true;
            }
            yield return null;
        }

        FirstResponses();
}

Replace “KeyCode.Space” to something else if you need to check on other keycode.

Hey,

so, the Coroutine will wait those 4 seconds no matter what. What you can do is:

  1. In Update(), check if uses pressed Key to skip the dialogue.
  2. Use StopCoroutine(Wait()) to stop the coroutine
  3. And then call FirstResponses() or SeconResponses() based on what response has already been given.
  4. Then you just skip to another dialog coroutine again, or end the whole dialog based on your needs.

Hope this helps!