- Home /
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.
Answer by 0916dhkim · Feb 08, 2016 at 05:24 PM
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.
Answer by Wrymnn · Feb 08, 2016 at 08:09 AM
Hey,
so, the Coroutine will wait those 4 seconds no matter what. What you can do is:
In Update(), check if uses pressed Key to skip the dialogue.
Use StopCoroutine(Wait()) to stop the coroutine
And then call FirstResponses() or SeconResponses() based on what response has already been given.
Then you just skip to another dialog coroutine again, or end the whole dialog based on your needs.
Hope this helps!
Your answer
Follow this Question
Related Questions
Problem with Pause Menu because of WaitForSeconds 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Pause a script completely 3 Answers
How to gradually increase the speed of a NavMesh agent? 2 Answers