Good Evening,
I am trying to create a script that will countdown from 5. Before the countdown, the script will wait a random number of seconds before saying “Go!”. During this countdown, if the user presses the key “x” before the timer has finished counting down then the user wins. However, if the user does not press x before the countdown completes, the computer wins. I tried doing this in a while loop but it didn’t work.
The variable seconds will almost immediately drop to below 0 setting cpuWin to true.
var cpuWin : boolean = false;
var playerWin : boolean = false;
var seconds : float = 5.0;
function Start () {
yield WaitForSeconds(Random.Range(1,10));
Debug.Log("Go!");
while(seconds > 0) {
if(Input.GetKey("x")) {
playerWin = true;
Debug.Log("You win!");
break;
}
seconds -= Time.deltaTime;
if(seconds <= 0) {
cpuWin = true;
Debug.Log("You lose!");
break;
}
}
//Once out of the loop do other stuff.
}
Sorry if im missing something here. Im getting back into programming before I go back to school and my logic and coding is very rough. Am I at least headed in the right direction? Or is there an easier way to do this?