Some of the best Game Over screens in the classic arcade games had some way of catching the attention of the next player. But since we are not building a game to go into an classic Arcade, we don’t have to try to get the next quarter. Instead we just want to capture that feeling. To this end, I’ve decided to go with a simple fade in and out of the ‘Game Over’ message.
Since we are using a special font, the same through out the game so far, we will be TextMeshPro again. So we will setup a TMP_Text variable as well as a Variable to control the flicker/fade speed.
Here you can see that I have set up both variables with the Serialize Field attribute to allow the design and myself access to them later. But I have also included a new Range attribute on the _gameOverFlickerSpeed. This adds a bit of ease of use to the inspector with the addition of a Slider while constraining the variable to set limits. With the variable being a float, we also want to make sure the Range limits are floats.
Let’s take a look at the whole Game Over Display method as a whole before we break it down.
Yes, Another Coroutine. Why? Because this effect will be something that changes over time and will repeat until something triggers it to stop. Now let’s look at it in steps.
In the first section, we prepare the display. We set the GameObject that the _gameOverText is attached to as Active. Then we set a variable for Alpha to 0 so that the text fades in to start. But you will notice that I am using a new variable type, Byte. This is a 8 bit integer that is limited to 0-255 which is perfect for with Color32 directly. Also Color32 is a variant on Color that defines the RGBA as a Byte or Int between 0–255. It is nice to work with when you are not trying to do math with Color, such as in a shader.
Lastly, we set the text color to full Red with no Alpha so that it is completely invisible when it is activated.
Next, after we enter the GameOver Loop, We start a loop to ramp the alpha up to full. At the end of each frame, we calculate the Delta Time for the frame and multiply first by 100 (because we are working in fractions of a second ideally around 1/90 or smaller) then by the Speed variable. Casting the whole calculation as a Byte cuts off any extra information after the decimal point to make the change a whole number. And we add this to the Alpha variable and use that in to reassign the RGBA to the _gameOverText. After the loop, We give a breath and set the RGBA to full Visibility before moving on to the fade out.
Just like with the Fade In, we do the same calculates except that we subtract them from the Alpha. And at the end, we add a pause for the frame before redoing the whole thing.
As with the Score and Lives counters, we are using an event to trigger the coroutine.
Here we set the _isGameOver to True so that the DisplayGameOver will loop properly. But we don’t bother setting it to false except for when we first declared it. This leaves us in a permanent Game Over State. In the next article, we will look into fixing that by setting up a Game Manager to track the state of the game and to all the player to restart when they are ready.