Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning C# by Developing Games with Unity 5.x - Second Edition

You're reading from  Learning C# by Developing Games with Unity 5.x - Second Edition

Product type Book
Published in Mar 2016
Publisher Packt
ISBN-13 9781785287596
Pages 230 pages
Edition 2nd Edition
Languages

Table of Contents (20) Chapters

Learning C# by Developing Games with Unity 5.x Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
1. Discovering Your Hidden Scripting Skills and Getting Your Environment Ready 2. Introducing the Building Blocks for Unity Scripts 3. Getting into the Details of Variables 4. Getting into the Details of Methods 5. Lists, Arrays, and Dictionaries 6. Loops 7. Object, a Container with Variables and Methods 8. Let's Make a Game! – From Idea to Development 9. Starting Your First Game 10. Writing GameManager 11. The Game Level 12. The User Interface 13. Collectables — What Next? Index

Chapter 10. Writing GameManager

We have achieved some basic gameplay. Now is the time to tie it all together. In this chapter, we will cover the following topics:

  • What is a gameplay loop?

  • What is a singleton class?

  • Writing GameManager events?

  • Implementing the first game loop

Gameplay loops


Well done so far. You have added basic functionality like jumping, physics, and running to the PlayerController object. We are definitely going in the right direction here. The next important step is writing a neat GameManager class to help us control the game events like:

  • StartGame

  • GameOver

  • Restart

For basic games like Jake on the mysterious planet, it is a good practice to have one instance of the GameManager running and controlling all main events in the game. The gameplay loop is simply a journey from the gameplay start to the gameplay phase and the game over phase. Time to write some code!

Let's create a new C# script and call it GameManager, and write the following code:

As you can see, nothing is very complicated. We wrote very simple methods to help us control the main game events. This script does nothing yet; however, please add it to the Unity Scene. Create a new game object call it GameManager, and add the GameManager component to it. We will use it in the future...

Singleton class


By implementing the singleton pattern for GameManager, we can easily access it from anywhere using one single point of access. I guess you will feel really confused about this now. A simple example will help you get your head around it.

 

"In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system."

 
 --Wikipedia

Let's add the following code to the GameManager class. Declare a new public static variable. This code should be written right next to other public variables:

public static GameManager instance;

Then, add an Awake method with the following line.

void Awake() {
  instance = this;
}

That's it! This is all the code you need for a simple access to the GameManager instance from anywhere in your code. It is important to remember that only one instance of this component can be present in the whole Unity Scene. To access any...

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...

Setting up input keys


One more thing that's missing now is adding s into Unity's build in InputManager. To do that, follow these simple steps.

  1. Open InputManager by going to Edit | ProjectSettings | Input.

  2. Increase input size of Axis by 1.

  3. Select the bottom Axis and change its settings.

We have a new input button set up as well as the code executed each time the button is pressed. Time to test that. Press Play in Unity and, after Jake drops on the platform, press S on the keyboard. The StartGame() method will be called by Unity just after you pressed the key. The StartGame() method changes currentGameState to inGame so our gameplay starts.

So, we completed the first part of the simple gameplay loop. The user can start the game by pressing the button and the game will start. As we are calling it a loop, it will have to be a closed chain of events. To close the gameplay, we will need to add the GameOver event.

In our simple game, the game over event will be called when the player dies. There will...

Using triggers


We can easily configure any collider in Unity to work like a trigger. Triggers are very useful. In this case, we will use them to detect whether our character has fallen into the hole. I have already prepared another useful prefab for you, so we won't waste any time setting it up. The steps are as follows:

  1. Import KillTrigger.unitypackage into your project.

  2. Drag Kill trigger into your project.

  3. Position the KillTrigger game object so the red area is below the ground.

This is all we need in the Scene view. Once Jake drops down from the end of the platform, he will most certainly fall through the red trigger zone. Now, we need to write some code to describe this behavior. It will be a very simple component added to the KillTrigger game object.

Create new a C# script, call it KillTrigger, and write the code so it looks like this:

As you can see, there is nothing complicated here. We use the OnTriggerEnter2D method. It is called automatically by Unity whenever another 2D collider enters...

Restarting the game


At this moment, we have a very simple gameplay. We can ask the game to start and we know when the player is finished. All that's missing to complete the game loop is the ability to restart the game.

Restarting the game should be done by the user by pressing the button on the screen or by pressing the button on the keyboard. Let's use the same input event we already have to start the game. The main difference between starting and restarting the game is that actual conditions in the game might be much different. For example, the player's position in the game will be different. In fact, this is a good starting point. Let's make sure every time the game starts, the player's initial position is the same.

Setting up the player starting position


Every time our game starts, we should reset all its conditions to the same state. We already mentioned that resetting the starting position of the Player game object would be a good start. Positions in the 3D world in Unity are described using Vector3 struct. Go ahead and type Vector3 in the Scripting Reference for a better understanding. This is complex stuff, so don't worry if you can't get it. All you need to know now is that Vector3 is made up of three floats describing x, y, and z positions in the space.

Let's go forward and perform some code changes to set up the Player position. In PlayerController, we will:

  1. Add private Vector3 type variable and call it startingPosition in PlayerController.

  2. Assign the startingPosition value taken from the Player game object world space position in the Awake method. This way, we will always store the initial position of the Player game object just after Unity starts executing the game.

  3. Rename the Start method to...

Code in this chapter


Code for GameManager.cs:

using UnityEngine;
using System.Collections;

public enum GameState {
  menu,
  inGame,
  gameOver
}

public class GameManager : MonoBehaviour {

  public static GameManager instance;
  public GameState currentGameState = GameState.menu;


  void Awake() {
    instance = this;
  }

  void Start() {
    currentGameState = GameState.menu;
  }
  
  //called to start the game
  public void StartGame() {
    PlayerController.instance.StartGame();
    SetGameState(GameState.inGame);
  }
  
  //called when player die
  public void GameOver() {
    SetGameState(GameState.gameOver);
  }

  //called when player decide to go back to the menu
  public void BackToMenu() {
    SetGameState(GameState.menu);
  }

  void SetGameState (GameState newGameState) {
    
    if (newGameState == GameState.menu) {
      //setup Unity scene for menu state
    }
    else if (newGameState == GameState.inGame) {
      //setup Unity scene for inGame state
    }
    else if...

Summary


In this chapter, we covered the basics of the singleton approach. We also covered the gameplay loop. You are doing really well. In the next chapter, we will talk about generating the levels.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learning C# by Developing Games with Unity 5.x - Second Edition
Published in: Mar 2016 Publisher: Packt ISBN-13: 9781785287596
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}