Niki.j
October 14, 2013, 1:09pm
1
Hello again all,
I am trying to implement the countdown “3->2->1-> Go” on resume the game with the code below:
IEnumerator getReady()
{
showCountdown = true;
countdown = "3";
yield return new WaitForSeconds(0.5f);
countdown = "2";
yield return new WaitForSeconds (0.5f);
countdown = "1";
yield return new WaitForSeconds (0.5f);
countdown = "GO";
yield return new WaitForSeconds (0.5f);
countdown = "";
showCountdown = false;
}
void OnGUI()
{
if(!isResumed)
{
if(GUI.Button(resumeRect,System.String.Empty,GUIStyle.none))//Resume Button
{
isResumed = true;
StartCoroutine(getReady());
Time.timeScale = 1.0f;
}
}
if(showCountdown)
{
resumeCountDownText.font.material.color = Color.red;
resumeCountDownText.text = countdown;
}
}
but when i run this code it will also resume my game and player can able to play the game, and if i delete Time.timeScale = 1.0f from resume button, then my resumeCountDownText will show only “3” and the countdown will not get start so that i can resume the game after completing the countdown.
please suggest where to call this StartCoroutine(getReady()) so that it will first do my countdown functionality and then resume the game for the player to play…???
Thanks in advance and sorry for the rent.
Niki.j
Try something like this:
function countDown(){
audio.pitch=.8;
audio.PlayOneShot(countSound);
levelText.material.color=Color.red;
levelText.text="GET READY";
yield WaitForSeconds(2);
audio.pitch=1;
audio.PlayOneShot(countSound);
levelText.material.color=Color.yellow;
levelText.text="GET SET";
yield WaitForSeconds(2);
levelText.material.color=Color.white;
startGame();
}
In an OnGui function you can check if button pressed or so, and then function countdown
In the Function on awake call Countdown
Maybe this helps?
Niki.j
October 15, 2013, 5:42am
3
1386428–70977–$BreakoutGame.cs (7.27 KB)
wandern3D:
Try something like this:
function countDown(){
audio.pitch=.8;
audio.PlayOneShot(countSound);
levelText.material.color=Color.red;
levelText.text="GET READY";
yield WaitForSeconds(2);
audio.pitch=1;
audio.PlayOneShot(countSound);
levelText.material.color=Color.yellow;
levelText.text="GET SET";
yield WaitForSeconds(2);
levelText.material.color=Color.white;
startGame();
}
In an OnGui function you can check if button pressed or so, and then function countdown
In the Function on awake call Countdown
Maybe this helps?
Hello,
i have tried this too, but this does not make any change in the execution.
It behaves like call the function/iEnumerator just for once, and when ever it execute yield it get stuck over there till the game get resumed(i.e Time.timeScale = 1.0f), if i resume the game and then call this function/IEnumerator, both countdown and game get start executing together, that should not happen.
What should happen is when player resume the game a countdown get start and when countdown get finished then game should start automatically…
please help if any one have any more ideas regarding this…
you guys can also check my complete code file attached here…
Thanks in advance!
Niki.j
While the countdown is active, Time.timeScale should still be 0 (paused).
In the Coroutine, you will need to keep track of Time.realtimeSinceStartup , checking against it every frame until it has passed a certain amount.
Untested Example:
IEnumerator getReady()
{
showCountdown = true;
countdown = "3";
yield return WaitForRealSeconds(0.5f);
countdown = "2";
yield return WaitForRealSeconds (0.5f);
countdown = "1";
yield return WaitForRealSeconds (0.5f);
countdown = "GO";
yield return WaitForRealSeconds (0.5f);
countdown = "";
showCountdown = false;
Time.timeScale = 1;
}
IEnumerator WaitForRealSeconds (float waitTime) {
float endTime = Time.realtimeSinceStartup+waitTime;
while (Time.realtimeSinceStartup < endTime) {
yield return null;
}
}
After the countdown is finished, reset Time.timeScale.
Niki.j
October 15, 2013, 7:45am
5
Hello,
DanielQuick:
While the countdown is active, Time.timeScale should still be 0 (paused).
In the Coroutine, you will need to keep track of Time.realtimeSinceStartup , checking against it every frame until it has passed a certain amount.
Untested Example:
IEnumerator getReady()
{
showCountdown = true;
countdown = "3";
yield return WaitForRealSeconds(0.5f);
countdown = "2";
yield return WaitForRealSeconds (0.5f);
countdown = "1";
yield return WaitForRealSeconds (0.5f);
countdown = "GO";
yield return WaitForRealSeconds (0.5f);
countdown = "";
showCountdown = false;
Time.timeScale = 1;
}
IEnumerator WaitForRealSeconds (float waitTime) {
float endTime = Time.realtimeSinceStartup+waitTime;
while (Time.realtimeSinceStartup < endTime) {
yield return null;
}
}
After the countdown is finished, reset Time.timeScale.
thanks for the suggestion…
i have tried this code too, but this also not working if time.timeScale = 0;
any other suggestion or any tested code would be very much helpful…
thanks
niki.j
I don’t usually do this (it’s usually more beneficial for you to learn how to fix it yourself), but here is the fixed code:
IEnumerator getReady()
{
showCountdown = true;
countdown = "3";
yield return StartCoroutine (WaitForRealSeconds(0.5f));
countdown = "2";
yield return StartCoroutine (WaitForRealSeconds(0.5f));
countdown = "1";
yield return StartCoroutine (WaitForRealSeconds(0.5f));
countdown = "GO";
yield return StartCoroutine (WaitForRealSeconds(0.5f));
countdown = "";
showCountdown = false;
Time.timeScale = 1;
}
IEnumerator WaitForRealSeconds (float waitTime) {
float endTime = Time.realtimeSinceStartup+waitTime;
while (Time.realtimeSinceStartup < endTime) {
yield return null;
}
}
1 Like
Niki.j
October 16, 2013, 6:28am
7
Hello Daniel,
Thank you so much for the help,it works perfectly.
Thanks
Niki.j