Starting the game

At the moment, our gameplay starts automatically after pressing the Play button in Unity. This was convenient for testing running and jumping. If you look into the Start method in GameManager, you will notice we are calling the start game there. Let's remove that line and keep the Start method empty for now.

Further in the development of this game, we will have a nice Graphic User Interface (GUI) to control the game states by pressing buttons like Start Game, Restart, and so on. For now, we should focus on functionality only and leave the GUI for later. However, we do need an easy way to call the events at runtime. Why not use the keyboard for now? You probably remember using Input.GetKeyDown. If you don't remember much, dive into Unity Scripting Reference again and search for Input.GetKeyDown.

Let's say when each time user presses S on the keyboard, we will fire up the StartGame method on GameManager. Before we start adding code, we need to make sure that currentGameState is set to inMenu just after pressing Unity button. To achieve this, simply edit Start method in GameManager:

void Start() {
  currentGameState = GameState.menu;
}

In Unity, after pressing Play, Jake has running and jumping disabled as the current state is inMenu. This is how we expect it to work now. Let's add more code to call the StartGame method on keyboard press. Write the Update method within the GameManager class:

Starting the game

With your coding experience, you can definitely understand what we are doing here. Every time the button S is pressed on the keyboard, the StartGame() method will be called.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset