Unity 2D Prototype part 3 – Basic Mechanics 2

      Comments Off on Unity 2D Prototype part 3 – Basic Mechanics 2

Now that we have some of the basic mechanics in place it’s time to make it look a bit more like a game. First off we’ll make it so that we can’t play anything until we actually press start.

First let’s add a few new canvases to our scene, the Game Screen and GameOver Screen. We’ll add corresponding GameObjects to the SceneManager and create Navigate methods for each one.

Update our existing methods to turn off the game and game over screens

Add the new methods to navigate to each screen

Create the new canvases so that the game screen has two text boxes, one for player health and one for score, and the game over screen has a text label for the game over text, and two buttons, one for retry and one for quit

Now that we have our UI set up we need to add in some controls so that the game starts when we hit start on the title screen. First wire up the NavigateToGame method to the button on the title screen.

Next up we need to disable the EnemyHitDetection and EnemySpawner components on the System object.

And we’ll enable these in the NavigateToGame method on our SceneManager script. We want to turn them on when we go to the Game screen, and back off when we go to the game over screen

The GetComponent<T> method allows us to get the specified script from the game object as long as that script was added as a component to the game object. If the script isn’t found then GetComponent returns null. We null check here because it’s a good habit to always check that an object returned from a function is valid but in this case unless we go delete the script component from the system object in the editor the results we get will never be null.

Next up, when the player runs out of health we want to navigate to the GameOver screen. We’ll update the GameInfo script so that when we run out of health we do the navigation.

And finally we’ll wire the buttons on the game over screen to navigate back to the game and title screens.

If you retry however you’ll find that the player can’t lose the second time because the health is either already 0 or is less than 0. We’ll update the GameInfo script to have max health and current health values.

And we’ll call the ResetHealth method from our SceneManager when we navigate to the game screen.

Now there’s one little bug you may have noticed remaining. If you spawned a lot of enemies on the screen, and hit retry too fast then the enemies will remain and we’ll lose health from enemies that are remaining from the previous game. When we navigate to the game screen we need to make sure to destroy any enemies that are still in the scene. To make this easier we’ll select our enemy prefab and change the tag in the inspector. A tag is an identifier that allows us to categories game objects so that they’re easier to identify later.

Add the enemy tag by choosing Add Tags under the tag dropdown, and set the tag of our prefab to Enemy when done.

In this new method ClearEnemies we search the hierarchy for game objects with the tag “Enemy” and destroy all of them. This is called before we Reset the System objects and restore the players health.

We’re almost done with our goals, the only thing left is to show the players health and score on the screen.

We’ll start by adding the score to the GameInfo object. Adding a new method to add to the score, and changing our ResetHealth method into ResetGame and making it set the score to 0 in addition to the health. Don’t forget to change the name in the SceneManager as well.

And finally we need to display the score and current health values on the UI.

Update the GameInfo script so that we have two public Text variables. These represent the textboxes in our game canvas. Once they’re added drag the textboxes into the appropriate variables in the editor.

We need to update the text on both of these whenever the score or current health are updated in the GameInfo script.

And there you have it. When you run the project you’ll have a working score and health display.