How I started with Unity multiplayer

Sridevi Pavithra
6 min readJun 13, 2020

--

Developing a multiplayer game is a common desire most game developers have. I too had this burning desire! When I started developing my first multiplayer game, I had a lot of difficulties choosing the right solution. Obviously! I was just a beginner and also, Unity has a very complicated relationship with its multiplayer solution. I’m writing this post in hopes that it can be useful to those who are trying to implement multiplayer functionality in games.

Concepts

Have you ever wondered how multiplayer games work? From the outside, it may seem magical, players connecting over the network and sharing the same experiences in the same virtual world. Programmatically, what you perceive as a shared world, is unique to you! Yes, it means, each connected player will have a local copy and only the states are transferred over the network. But how?!

Peer-to-Peer (P2P)

Devices in the network are directly connected. It’s a distributed architecture. There’s no concept of a centralized server. It establishes a “direct” communication among the players. Beware, P2P is a tricky technique to get right!

Of course, you’ll be saving up a lot by not having a dedicated server but this would also mean you’re opening up ways to cheat and tamper.

A Classic Server-Client Architecture

Now instead of each player communicating directly with each other, each player is a “client”, and they all communicate with just one computer called the “server”. All messages have to go through the server and can’t be sent directly to other players. An authoritative server is where only the server can make changes to the game.

A player typically sends a command from “player gameobject” to the server “gameobject”. The server runs it and sends the result to all connected players through ClientRPC. Remember? Each player will have a local copy of the game, which is where client authority comes in. It makes sense, right? I shouldn’t “command” over other players. I can only move what’s mine.

So what if the response is taking longer due to network traffic? All that a client does here, interpolate between the results from the server and give out an illusion of smooth movement. And that’s how an authoritative network game works ;)

Run as host

Okay, I get what server-client is, now what if I let one of my players act as a server instead of having a dedicated server? This technique is called running as Host. Best suited for LAN games!

Explore, learn, and build!

When I first came across these concepts, all I could think was — “Wow, I now know the concepts! It’s simple and I can develop this easily. So, let’s get on with it”. Ha! Now I know how few things are easier said than done!

Developing a game can be quite challenging for a beginner, but bringing multiplayer into it can be daunting. Fret not, what once was a formidable task to perform, has been made a lot easier with multiplayer plugins like Photon, Mirror, etc.

When you know better, you do better!

To give you a headstart, here are some of the solutions I explored. Hey! these are merely suggestions, take it from here, do some more research and then decide :)

1. Mirror Networking

UNet is Unity’s legacy multiplayer solution. I preferred UNet back then. It was disappointing when Unity decided to deprecate it. Of course, it’s obvious why they did what they did. But hey, when one door closes, another opens ;)

With a server-client architecture, Mirror is what UNet should’ve been! It’s a high-level API that’s easy to integrate. It comes with different low-level network transport options and it’s easily scalable!

2. MLAPI

As the name suggests, it’s a mid-level networking API for Unity! Based on server-client architecture. What’s wonderful is that it comes with many features like RPC return types, dictionary syncing, etc., and offers more customization. If you’re already familiar with networking and looking for something more, add this to your list.

3. PUN

Photon Unity Networking, a fast and reliable network package. You can try out the free-plan that offers up to 20 concurrent users. An interesting factor is that even though, PUN has a server-client architecture, it supports P2P. This is exactly why it’s faster than Unity’s networking solution.

Hey! If you’re looking for something a bit more advanced, check out Dark Rift 2, a powerful multiplayer library. It requires a cloud-based authoritative server and it’s excellent!

Frankly speaking, the list goes on! But what good is a plugin when you have no clue what to do with it?! If you’re just getting started, pick the one that has proper documentation. Trust me; you NEED references while developing!

Design to solve problems

Concept to implementation is usually easy to learn but hard to master!

Unity seems easy, doesn’t it? Simple drag & drop can handle a lot of things. There’s more to it than we know, and some things can be potentially dangerous. Too many “Find” and “Get” and unwanted update calls. Finding an object in the hierarchy and accessing it, has it’s overhead. Especially when you’re building a multiplayer game. Another common mistake we do is unintentionally make our code depend on each other. I’ve been there, done that!

I kept coming back to a project within a few months’ gaps, and I realized that it’s harder to read code than write it! I used to convince myself, saying, I would’ve written better only if I had had time. NOT TRUE. Make time to think, design, and organize your code. It’s better to take a little more time and write a clean code than rush it to end up with a dirty code. I’m no expert when it comes to design patterns, but I try! But again, don’t obsess and overdo it. Sometimes, following a proper naming convention and writing modular code is sufficient. You can always get back it and refactor it. Keep it modular, keep it simple :D

Going “LIVE”

You need a dedicated server. Period.

There are three ways you can take your game “Live”

  1. Use a multiplayer service for development and hosting. Photon PUN, SocketWeaver, and Player.io are some of the cool multiplayer services out there that offer free hosting with CCU limits.
  2. Use a service provider for just hosting. I prefer using just a network library for development. It gave me more control over how I want to manage. It was also easier for me to toggle between LAN & online. For hosting, I decided to go with a provider. I remember reading blogs and articles regarding this before I ended up using AWS EC2’s free tier for hosting. It only took an hour to set up the entire thing! There’s also Microsoft Azure, and google cloud computing that you can look into if you’re just getting started.
  3. There’s always self-hosted dedicated server for those who can afford and maintain it :D

Don’t forget to take a headless server build!

Here’s a tip — it’s fun to play an online multiplayer game with friends. But, why do you think PUBG & Fortnite are as famous as they are right now? Multiplayer, yes! And also because of the game communications. Engage players better with voice chat. There are some cool VoIP solutions out there like Dissonance, Vivox, etc. for Unity.

All the best, developing your first multiplayer game.

Feel free to reach out to me on LinkedIn using this link for further assistance on how. :)

--

--

Sridevi Pavithra

I’m a gameplay-programmer. To save time, let’s just assume my code is always bug free :)