Chapter 6. Networking and Multiplayer

In previous chapters you learned how to use Unity's interface, manipulate objects, and add components and behaviors to them. You created gameplay using Playmaker actions and Unity scripting. We also looked at making custom actions from C# scripts. You made a fully playable air hockey game with an AI opponent using all these tools.

In this chapter, we are going to talk about networking. You will make a multiplayer mode for the game using Photon Unity Networking (PUN), which is a helpful plugin that comes with Playmaker and lets you make multiplayer games almost effortlessly. We will also talk about the theory of networking in games and discuss Unity native networking as an alternative to Photon.

In this chapter, you will cover:

  • Understanding networking and multiplayer
  • Setting up Photon Unity Networking
  • Making a multiplayer game

Understanding networking and multiplayer

Explaining things like TCP/IP and other low-level networking concepts is beyond the scope of this book, and we are going to try and keep everything as close to practical application as possible. On the other hand, it will be much easier for you to build multiplayer if you are familiar with at least some theory.

The first thing that you need to know is what servers and clients are. In simple terms, a server is a computer that responds to network requests from other computers, or more precisely, a system that responds to network requests from other systems, because you may have multiple servers and multiple clients on the same computer. This means that clients communicate with each other through a server. Typically, player systems are clients in multiplayer games, while servers are on computers that are accessed remotely. Sometimes, a player can host a game, in which case the player either acts as a server or simply tells the server to reserve its resources for the game.

You may have heard about network architectures before. In games, the most popular architectures are arguably client-server and peer-to-peer. The former means that all the clients subscribe to a single server. The server hosts most of the important information about the game and distributes it between the players. The latter is about peers (players) connecting to one another directly, so all the clients are connected to each other, and the network workload is distributed evenly.

The advantage of client-server is that it allows creating a more stable system that ensures that cheating is either impossible or very hard, as well as makes it easier for the developer to monitor everything and make changes to the game on the fly. This method, however, is generally expensive and relatively hard to implement yourself if you are just a solo developer who is learning to make his/her first multiplayer game.

Peer-to-peer does not require having a powerful server hosting multiple game sessions at the same time, allowing players to connect to each other, distributing the network workload between them. The drawback of peer-to-peer is that it tends to be less stable, and it is relatively hard to monitor. On top of that, you still need a server if you want to keep track of game sessions, perform matchmaking, and let your players play on the Internet instead of just a local network.

Tip

It is hard to connect to the Internet without having a server, because of something called Native Address Translation (NAT). Without getting into much detail, it should be noted that it is something that network routers do, and most of us have routers these days. A process commonly called NAT punchthrough is used in order to connect one computer to another, and this process requires a server acting as a mediator for the first connection between two computers.

In Unity, it is relatively easy to set up a Local Area Network (LAN), peer-to-peer connection, or client-server connection with one of the players hosting the game without using any external plugins. A LAN connection means that all the players are connected to the same local computer network. Unfortunately, whatever you do, you will need a server to make sure that players can always connect to each other over the Internet, which is what we want to do. Photon takes client-server and wraps it in an extremely easy to use interface that allows anyone to create multiplayer games without any prior experience. On top of that, it is very affordable for what it is.

Some of the Photon services allow players to host their own servers, but those take some time to set up. What we are going to use in this book is Photon Cloud. As the name implies, all the game sessions happen in the cloud, thus on remote Photon servers. All you need to do is synchronize your game's data through them and make sure that players can find each other. You do not need to set up a server, and you do not have to worry about the problems involved in making peer-to-peer multiplayer.

The way the synchronization works is that there is something called Network View (Photon View in Photon), which is a component that makes one or more game object's properties synchronized over the network. When such a property changes in one client, it changes in all the clients, with the server keeping track of everything and sending commands to clients. For example, mallet's position can be such a property. This way, when player 1 moves their mallet, player 2 sees them move it and vice versa. The same goes for almost any other property.

Tip

As we will show in this chapter's example, synchronizing positions can be okay for player objects (such as mallets in air hockey or characters in first person shooters), but objects that have physical behaviors (such as the puck), can experience serious network delays on remote computers. There is currently no easy solution for this problem apart from using Unity native networking.

There are objects that can belong to the scene (like the walls and the background) and so are exactly the same and unchangeable throughout all clients, and then there are objects that belong to different clients, such as the mallets. Generally, you want to synchronize as little data over the network as possible in order to avoid high response times. Scene objects do not have to get synchronized since they do not change. Moreover, this way each player's mallet responds only to that player's input, which makes perfect sense gameplay-wise.

You are now ready to start setting up Photon. All this theory may sound complicated, but it really comes down to synchronizing variables over the network using a special Photon component called Photon View.

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

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