Creating a Retro Game Over Behavior
Objective: Learn how to create a retro game behavior.
With our UI System now implemented, we want to add the Game Over functionality over it. That means having a Game Over text on the screen, that we want to flicker over and over, showing that the game is over.
[SerializeField] private TextMeshProUGUI _gameOverText;
We can create a Text that will hold that information and assigning it in the Inspector. But how do we implement such functionality ? How do we know when the game is over ?
public void UpdateLivesDisplay(int currentLives)
{
_livesImage.sprite = _livesSprites[currentLives];
ActivateGameOver();
}
Inside our UpdateLivesDisplay method, we have the information for how many lives there are currently on the player, meaning we actually have information if the amount of lives becomes zero. This allows us to call a method that will handle the details of what happens when the game is over, from the UIManager perspective.
private void ActivateGameOver()
{
_gameOverText.gameObject.SetActive(true);
StartCoroutine(GameOverFlickerRoutine());
}
First we make sure that we activate the GameObject that contains the text for our Game Over, and then we are going to start the coroutine that will handle the Flicker of our text.
Why are we using a Coroutine you might ask ? Well, instead of having to animate our Game Over text, we can simply turn it on/off every few seconds, and the way for us to manipulate time is through the use of Coroutines.
private IEnumerator GameOverFlickerRoutine()
{
while (true)
{
_gameOverText.text = "GAME OVER";
yield return new WaitForSeconds(0.25f);
_gameOverText.text = "";
yield return new WaitForSeconds(0.25f);
}
}
The coroutine above is activated when our game ends, meaning our player has no more lives left. What does this Coroutine do ? Well, it literally flickers the text on the screen every 0,25 seconds, meaning it will flicker very fast, giving an old videogame vibe!
