Skip to content
spaceapplesoft edited this page Mar 17, 2021 · 14 revisions

Multiroom PvP example

ℹ️ If you think there's something missing in documentation or if there are topics you want me to cover in more detail, please let me know and I'll update it - your feedback helps me to improve this document :)

ℹ️ If you're unfamiliar with Easy Packets, it's probably a good idea to take a quick look at its documentation. You'll find it in SpaceApple directory

What is this?

🔥 Warning: After UNET was deprecated, this asset has been rewritten to use Mirror networking. Mirror networking is now necessary to run this: https://assetstore.unity.com/packages/tools/network/mirror-129321

Multiroom PvP is a Unity asset which serves as an example on how to use Mirror Networking to create multiple game rooms in a single Mirror server. It's not meant to be a complete project, but more like a learning resource to get you inspired or help you increase your knowledge of networking with Mirror/UNET. For this reason, it was decided not to add a bunch of fancy effects and game modes, and just focus on the core functionality.

What's included in the package?

  • Simple PvP game
  • Easy Packets asset - to make working with UNET more convenient

Why Multi-room setup?

If a game room is small, it might make more sense to have multiple game rooms in a single server. Take, for example, a small 1 vs 1 game. A small Unity game consumes about a 100 MB of RAM. You could only run 10 instances (rooms) in a 1 GB RAM server, which is wasteful and only allows 20 players to play concurrently. If all of your rooms are on one unity instance, you no longer have the overhead of Unity, and can run hundreds of game rooms. With this example, I've tested 200 players constantly running, and here are the stats: [200 players, 26% CPU, 184 MB RAM on a 5$/month VPS.]

What is the multi-room setup good for?

  • Arena-type PvP games (or what's frequently refered to "IO" (I'll probably burn in hell for this) games)
  • One vs One games

What are the limitations of this approach?

Ask yourself a question - does it make sense to copy the room multiple times in one server? Will it break something? (navmesh, terrain and etc.) For example, if terrain is large, it wouldn't make sense to multiply it in the server. If your game relies on having AI (bots), can you make sure that your navmesh implementation works on duplicate rooms?

Launching The Example

  1. Import Mirror (https://assetstore.unity.com/packages/tools/network/mirror-129321)
  2. Make a build with SpaceApple/Multiroom.PvP/Scenes/Multiroom.PvP scene.
  3. Run the build and hit LAN Host button
  4. Run other builds and hit LAN CLIENT button

Controls: WASD - movement, press/release spacebar to shoot.

Structure of the project

  • SpaceApple/Multiroom.PvP - contains all of the Multiroom example filed
  • All the other folders - are part of the Easy Packets asset. It has separate documentation with more information.

Overview of basic principles

As most of the multiplayer games, this example consists of two main parts: server and client. For the example to work properly, there has to be one Unity instance running as a server, and other instances - as clients.

  1. When "Play" button is clicked, a request is sent to the server. This request is handled in PvpGameServer.HandlePlayRequest.
  2. Game server then uses the room template and instantiates (duplicates) it. A player is added to that room, and responds to clients request with a position of the room.
  3. Client retrieves the position of the room where he should be playing, and also instantiates a copy of the room at the same position. This way, both client and server has a room created at the same position, so their versions of the game and colliders match.
  4. When client instantiates a room, it sends a notification to server, to let it know that client is ready to play (this is useful in case you need to load more stuff in the room and it takes a bit longer)

Area of interest management happens with two scripts:

  1. RoomInterestManagement- contains the logic for rebuilding observers list
  2. RoomObject - this should be added to all the networked objects in the room. It allows to store current room on an object. It notifies the room when an object is spawned and is destroyed, so that room can keep a live list of spawned objects.

⚠️ When an object (with NetworkBehaviour) is spawned, it should set the RoomObject.CurrentRoom as soon as possible. You'll see that in PvpModeController.SpawnPlayer method it is invoked right after instantiating the player.

Is it possible to have multiple room templates?

Yes. There are many ways to do it. The most straightforward approach would be to change the PvpGameServer.HandlePlayRequest method to use different templates, or you could send a different message (with different OpCode) and handle it separately (which is a cleaner approach).

Is it possible to have different game modes?

Yes. You could copy the PvpModeController, make changes and add it to another template, or replace it in the main room template.

Or you could write your own controller from scratch. Check out the PvpModeController to see how some of the events are handled and how the game is started. With a little bit of work, it's possible to make all types of game modes.