- Home /
How can add “Warm up” in unity 2d game?
Hi everyone! I want add "Warmup" to my game like Counter strike Warmup. I want that after 30 sec game start and restart player to original position and there is not timer anymore in the game. How can I Do? I use this line too but not working:
if (cuurentTime == 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
but is not working. I use this cod:
public class TimerCountDown : MonoBehaviour
{
float cuurentTime = 0f;
float startingTime = 30f;
[SerializeField] Text CountDownText;
void Start()
{
cuurentTime = startingTime;
}
void Update()
{
cuurentTime -= 1 * Time.deltaTime;
CountDownText.text = cuurentTime.ToString("0");
if (cuurentTime <= 0)
{
cuurentTime = 0;
}
}
}
What sort of not working? Does it count down? Does it not do anything at 0?
Answer by esparow · Feb 19, 2021 at 10:02 AM
this method would be better for a 2d game.
using System.Collections;
using UnityEngine;
public class TimerCountDown : MonoBehaviour
{
private void Start()
{
StartCoroutine(StopAndStartGame());
}
IEnumerator StopAndStartGame()
{
Time.timeScale = 0; // for stop game
yield return new WaitForSecondsRealtime(30); // wait 30 second (realtime.. Because game is stoped.)
Time.timeScale = 1; // for start game
}
}
OR
using UnityEngine;
using UnityEngine.UI;
public class TimerCountDown : MonoBehaviour
{
float counter;
[SerializeField]
Text textCounter ;
bool warmupOn;
private void Start()
{
counter = 30;
warmupOn = true;
}
private void Update()
{
if (warmupOn == true)
{
counter -= Time.deltaTime;
textCounter.text = Mathf.Ceil(counter).ToString();
Debug.Log("starting");
if (counter == 0 || counter < 0)
{
Debug.Log("stoped");
warmupOn = false;
// Load Scene or Your Restart Method.
}
}
}
}
This won't allow the game to run normally during warmup. It will much rather freeze everything for 30 seconds which is not what OP is asking for.
Hi. Tanks for your answer. but I dont want the game to freeze. i want the player to be able to play for 30 sec but then the player returns to the original location and play normal.