Post tutorial RSS Final Foe. About developing network multiplayer with Unity

Hello! Today I will talk about how I added online multiplayer to my game Final Foe. Without network programming experience and without financial costs. Hope you enjoy!

Posted by on - Intermediate Server Side Coding

Part 1. Multiplayer game - what is it?

In my previous article, I wrote about the Final Foe project. Therefore, today I will focus directly on the multiplayer. For me personally, playing with friends is much more interesting than playing alone. That's why I originally wanted my game Final Foe to have multiplayer.

In fact, there are 3 most common multiplayer game options:

Local multiplayer
A variant in which two or more people play at the same time on the same device. Not to be confused with local area network (LAN) play. Local multiplayer doesn't need to transfer data over the network. Development is almost the same as developing a single-player game. By the way, Steam has a great Remote Play Together feature specifically for such games, which allows one player to connect to another, see his screen and control it remotely. A kind of TeamViewer for games.

Host Client
It is also Listen Server. One of the players is both a server and a client, i.e. host. The rest of the players are only clients. The host turns on the server on its own computer and connects to itself. Clients connect to the host. All network data between clients is transmitted through the host. The host always has zero ping, remote clients have higher ping.

Dedicated server
There is a separate independent server that players connect to. All network data between players is transmitted through it. Key game logic is also handled on this server, so there are far fewer opportunities for cheating. All players are on an equal footing. Such a server requires separate hosting and ongoing support.

ss 2adb6e5d49ea3c7bb046e2665565d4201

Each of these options has its own advantages and disadvantages, so the choice always depends on what kind of game you have.

With Final Foe, my goal was to make co-op multiplayer up to 4 players. At the same time, each player had to be able to move without being tied to the visibility zone of another player. This meant that the local multiplayer was not suitable for me.

A dedicated server, for all its advantages, would require me to constantly pay for hosting and constantly administer, and possibly scale this server. This meant that after the release of the game, I would have to regularly spend money and time maintaining the multiplayer in my game. And given that I planned to sell my game on the principle of a one-time sale (Buy-To-Play) - one day there might come a moment when no one buys the game anymore, and I have to maintain dedicated servers for previously purchased copies of the game. And at that moment, the mathematics of income and expenses would begin to work against me.

But the "Host-Client" option gave me a number of advantages. First, Final Foe is a PVE oriented game. Players in it must fight on the same side, fighting off monsters. This means that a small difference in network latency between the host and connected players is not as critical as in PVP-oriented games. Second, the Host-Client option does not require a separate server to be maintained all the time. If several friends want to play Final Foe, then one of them simply starts the game as a host, and the rest connect to it. And that's it, they play.

Thus, I decided to use the "Host-Client" option. Therefore, further we will talk about it. If you want to learn more about the types of multiplayer games, I recommend watching this video.

Final Foe - Multiplayer Gameplay Video.

Part 2: Affordable Networking Solutions.

In order not to write the logic of client-server interaction yourself, you can use a ready-made network solution.

I'll walk you through a couple of networking solutions for Unity. However, you probably know that there are many other game engines: Unreal Engine, Godot, Unigine and others. It is quite possible that some other engine will suit you, with some other network solutions. For example, in the Unreal Engine, a network solution is available right out of the box. At some point, I even wanted to move the Final Foe project to Unreal Engine because of this. Comparing Unity and Unreal Engine is a topic for a separate article. And this topic is not as simple and unambiguous as it might seem. For me personally, both Unity and Unreal Engine are very powerful and functional tools. With their advantages and disadvantages in relation to each other.

Generally speaking, a "network solution" (network library) is an additional asset with a code library in which all the functionality for transmitting data over the network is already written. You just have to use it in your game logic, following the documentation and tutorials. Download, add to the project. And after that, you can immediately do what you need: synchronize the movement of characters between clients, synchronize animations, visual effects, sounds, variables, methods, and much more. Literally with a few lines of code. And sometimes, by simply adding a certain component to the game object. There is no need to manually generate and send data packets. It's already written for you. Of course, you will need to study the documentation and understand what's what. But it's still much easier than writing absolutely all the network logic for your game from scratch.

ss 0e11052ba69750c9737169f3b3dd3a92f

Over a year ago, the Unity Technologies team did an overview comparison of several networking solutions for Unity. I will briefly review three of them.

Photon PUN 2
It is quite easy to integrate into the project. Photon has its own network infrastructure where you can create a lobby to connect between players. Therefore, you will not need to worry about how players will find each other online and connect to each other. However, if your game has more than 100 players at the same time, you will need to pay from $95 per month. The more players, the higher the cost.

Easy integration. Clear and helpful documentation. It is possible to use Steam network transport. There are tutorial videos from DapperDino on integrating into the Steam network infrastructure to connect players to each other using the network lobby. Huge and friendly community on Discord, where I have repeatedly received useful answers to important questions. The Mirror Network Library has extremely positive reviews on the Unity Asset Store and has a reputation for being a reliable free networking solution.

At the moment, this is the official networking solution from Unity. It came out not so long ago, so I have not yet had a chance to work with Netcode and I only judge by the data that I managed to find out. Netcode is based on the MLAPI network library and is being actively developed. More and more training videos on this networking solution appear on the Internet. The official website says that setting up a Netcode connection to the Steam network infrastructure is quite simple. It would be interesting to test this network solution in the future.

Part 3. Mirror + Steam.

The fact is that developing network logic for a game is only half the battle. It is important to understand that creating a direct connection between a host and a remote client is not as easy as it might seem.

Without using a static IP address and correctly configured ports, there is a good chance that it will be quite problematic to establish a connection. Players may need to use additional programs like Hamachi to emulate a local area network between them. Will your players want to do this? Unlikely. Therefore, for the selection and connection of players in all modern games of the "Host-Client" type, special intermediate servers are usually used. The so-called "lobby". In a predefined network infrastructure.

Steam also has such a network infrastructure. Games released on Steam - may use the Steam network infrastructure to connect players to each other. The host player creates an online lobby, and other players can connect to it through this lobby. Very comfortably. And most importantly, it's free. In general, Steam has quite a lot of useful features for developers. And the company Valve in this regard is worthy of praise.

Given that the best option for me was a free network library with the ability to use Steam network transport - I chose Mirror. And at the moment I'm very happy about it.

To be honest, almost until the very final stage of game development, I was not sure that I would be able to make multiplayer, and in particular - to set up interaction with the Steam network infrastructure.

In about 4 months, I managed to add Mirror-based multiplayer to my game. And it took another 1-2 months to set up interaction with Steam. It could probably have been done faster. Can't say I was in a hurry.

Part 4. Afterword

Less than a week left until my game Final Foe is released on Steam! So it's time to add it to your wishlist :-)

This article turned out to be more of an overview. And it does not contain technical instructions for the development of certain network functionality. For such things, there are training videos and official documentation. However, if you have any specific questions, I will be happy to answer them in the comments!

Join the social networks of my studio Alekliart. Thank you for your attention!

My studio Alekliart in social networks:

VK | Discord | Twitter | Steam | YouTube

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.