Book Image

Unity Multiplayer Games

By : Alan R. Stagner
Book Image

Unity Multiplayer Games

By: Alan R. Stagner

Overview of this book

Unity is a game development engine that is fully integrated with a complete set of intuitive tools and rapid workflows used to create interactive 3D content. Multiplayer games have long been a staple of video games, and online multiplayer games have seen an explosion in popularity in recent years. Unity provides a unique platform for independent developers to create the most in-demand multiplayer experiences, from relaxing social MMOs to adrenaline-pumping competitive shooters. A practical guide to writing a variety of online multiplayer games with the Unity game engine, using a multitude of networking middleware from player-hosted games to standalone dedicated servers to cloud multiplayer technology. You can create a wide variety of online games with the Unity 4 as well as Unity 3 Engine. You will learn all the skills needed to make any multiplayer game you can think of using this practical guide. We break down complex multiplayer games into basic components, for different kinds of games, whether they be large multi-user environments or small 8-player action games. You will get started by learning networking technologies for a variety of situations with a Pong game, and also host a game server and learn to connect to it.Then, we will show you how to structure your game logic to work in a multiplayer environment. We will cover how to implement client-side game logic for player-hosted games and server-side game logic for MMO-style games, as well as how to deal with network latency, unreliability, and security. You will then gain an understanding of the Photon Server while creating a star collector game; and later, the Player.IO by creating a multiplayer RTS prototype game. You will also learn using PubNub with Unity by creating a chatbox application. Unity Multiplayer Games will help you learn how to use the most popular networking middleware available for Unity, from peer-oriented setups to dedicated server technology.
Table of Contents (14 chapters)
Unity Multiplayer Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

This book intends to step you through the concepts and middleware involved in creating multiplayer games with the Unity game engine. I've been a big fan of multiplayer games for a while. They have a way of tapping into our basic desires, fulfilling a need to compete, to co-operate, and most of all to socialize with our fellow humans, in a way that no single player game can ever provide.

I've experienced a wide range of networking plugins and applications in Unity. As I learn new networking systems, there are always stumbling blocks and difficult issues. I wrote this book because I wanted to help others on the same path, and help them surmount the issues I encountered myself.

Unity IDE crash course

To better understand this book, we'll need to cover the basic features of the Unity IDE.

If you open Unity for the first time, you'll be presented with a window where you can either open an existing project or create a new one. Select the Create New Project tab, and choose a location for your project.

Once your project is created, you'll see a number of panels. There are the Scene and Game tabs, the Hierarchy, Project, and Console tabs, and the Inspector tab.

The Scene view shows the current scene. This will allow you to navigate the scene, select objects, move them around, and more. The Game view shows the view of the main camera. If you press the Play button, the Game view is automatically shown and allows you to play test your game from inside the editor.

The Hierarchy tab shows the object hierarchy of the current scene. This allows you to select objects, parent or unparent them, delete them, rename them, and much more.

The Inspector tab shows the editors for each component attached to the selected object (rather than being inheritance based like many traditional engines, Unity is component based, where objects are a collection of components and each component has a separate responsibility). It allows you to set values and change properties of components. You can also remove components by right clicking on a component and clicking on Remove Component. In Unity 4 and later, you can also click on the Add Component button and select a component script.

The Project tab shows the assets in your project. You can drop game assets here to import them, and you can create new materials, scripts, and shaders by right-clicking and selecting the Create option. You can also drag objects from the Hierarchy to the Project tab to create a prefab. Prefabs are essentially object templates—you can Instantiate a prefab to create an exact copy of the prefab in the scene (for instance, you might create an Enemy prefab, and instantiate it to spawn enemies). You can also drag component scripts from the Project to the Inspector of a selected game object to add the component to the object.

To learn more about Unity, you can get started here:

http://unity3d.com/learn

What this book covers

Chapter 1, Unity Networking – The Pong Game, introduces the concept of reliable UDP communication, and different types of servers employed by games. It covers Unity Networking, and creating a networked two-player Pong clone.

Chapter 2, Photon Unity Networking – The Chat Client, covers a third-party alternative to Unity Networking. It introduces the concept of the cloud-hosted game servers, simple matchmaking, and friends lists. It also covers the creation of a simple chat client.

Chapter 3, Photon Server – Star Collector, introduces dedicated servers for games. It covers creating a Photon Server application, connecting to a server, using the request/response/event system to communicate, and creating a simple Star Collector game.

Chapter 4, Player.IO – Bot Wars, covers an alternative dedicated server system. It introduces the database features of Player.IO, how to create a Player.IO server, connecting from Unity, and creating a simple RTS-style game with persistent user stats.

Chapter 5, PubNub – The Global Chatbox, introduces communication over an HTTP messaging service. It covers benefits and pitfalls of HTTP for communication, using the WWW class to communicate via PubNub, and creating a chatroom application.

Chapter 6, Entity Interpolation and Prediction, introduces the concept of server-side movement physics and potential issues and solutions. It covers client-side movement prediction, and how to smooth the motion of remote entities.

Chapter 7, Server-side Hit Detection, introduces the concept of server-side hit detection for shooter-style games. It covers the reasons behind target-leading problems in many online games, and how to resolve the issue by rewinding the game state.

What you need for this book

You will need Unity 3 or later for this book. Many chapters require specific downloads:

  • Chapter 2, Photon Unity Networking – The Chat Client, requires the Photon Unity Networking plugin

  • Chapter 3, Photon Server – Star Collector, requires the Photon Server client and server SDKs

  • Chapter 4, Player.IO – Bot Wars, requires the Player.IO developer package

  • Chapter 5, PubNub – The Global Chatbox, requires a third-party JSON parser

Instructions to download the required materials are covered at the beginning of each chapter.

Who this book is for

This book is for developers who want to get started writing multiplayer games with the Unity game engine. Readers are expected to have a working knowledge of C#. Knowledge of the Unity IDE is helpful, but not strictly required.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "Navigate to the Release folder and run the EXE."

A block of code is set as follows:

public class ExampleUnityNetworkSerializePosition : MonoBehaviour
{
  public void OnSerializeNetworkView( BitStream stream, NetworkMessageInfo info )
  {
    // we are currently writing information to the network
    if( stream.isWriting )
    {
      // send the object's position
      Vector3 position = transform.position;
      stream.Serialize( ref position );
    }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

// the maximum score a player can reach
  public int ScoreLimit = 10;

  // the display test for player 1's score
  public TextMesh Player1ScoreDisplay;

  // the display text for player 2's score
  public TextMesh Player2ScoreDisplay;

  // Player 1's score
  private int p1Score = 0;

New terms and important words are shown in bold. Words that you see on the screen, in menus, or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.