I played the game Ready-Steady-Bang recently and tried myself just to write a basic code and give it a try.So I used coroutines and Debug.log to play the game in editor.However as I decrease the time it takes for enemy to shoot. The problem is I lose immediately.Is it because I am playing with a keyboard vs touch on mobile phones or something else?Also sometimes it happens even though Bang has printed It takes quite a large time for me to press Q and win?What is that I am doing wrong?Here is the code-
public class Bang : MonoBehaviour
{
void Start ()
{
StartCoroutine(Game(0.2f));//provide the enemy speed!
}
IEnumerator common()
{
yield return new WaitForSeconds(2.0f);
Debug.Log("Lets start again");
yield return new WaitForSeconds(1.0f);
}
IEnumerator Game(float opponentTime)
{
float timer;
float waitForBang;
bool winner;
bool breaked;
while(true)
{
waitForBang = Random.Range(1.0F,4.0F);
winner = false;
timer = 0;
Debug.Log("Ready");
breaked = false;
if(!Input.anyKey)//stop player
{
Debug.Log("Steady");
while((timer<=waitForBang))
{
if(Input.anyKey)
{ breaked = true;
break;
}
timer+=Time.deltaTime;
yield return null;
}
timer = 0;
if(!breaked)
{
Debug.Log("BANG");
while(timer<=opponentTime)//here is the time to shoot for the player
{
if(Input.GetKey(KeyCode.Q))
{
winner = true;
break;
}
timer+=Time.deltaTime;
yield return null;
}
if(winner)
{
Debug.Log("you win");
//Enemy killed and your win animation here
}
else
{
Debug.Log("you lose");
//You killed and enemy win animation
}
yield return StartCoroutine(common());
}
else
{
Debug.Log("you lose");
yield return StartCoroutine(common());
}
}
else
{
Debug.Log("you lose");
yield return StartCoroutine(common());
}
}
}
}