Create multiplayer P2P

Hi,
My friend and I are struggling with multiplayer implementation. We want to create a peer-to-peer (P2P) multiplayer system.

More specifically, at the beginning of the game, right after the main menu, we want players to have the option to choose between “Create a Lobby” and “Join a Lobby.” The player who creates the lobby becomes the host (acting as the server) and receives a unique code to share with friends so they can join the lobby.

The game requires 2 players to start. Initially, the two characters will be together in the same scene, but only for a short amount of time. After that, for most of the game, the two players will be on two separate maps, until the final scene where they will meet again.

Finally, we’d like to achieve this without spending money at first.

Is there a question you forgot to ask? :wink:

Absolutely. Free tiers everywhere.

Hello @DEVOYEURS

What are you struggling with specifically? What have you tried and is not working? What is your experience with making games in Unity?

What you describe is in large part covered by multiplayer documentation in many game engines. It’s called peer to peer networking which should usually be done with a listen server. Steam relay (free) or Unity Relay (free-tier and paid-tier) are good options. Overall Unity has a lot of capabilities to support easy multiplayer networking with generous free-tier pricing for your proposed architecture.

You’d want to use Unity Lobby to define and list game sessions to join followed by Unity Relay to synchronize gameplay. You don’t have to send traffic when players are in different scenes, which could entail disconnecting during that part of the game or just keeping a heartbeat going that maintains the connections, but you significantly reduce sending data for players to only what’s needed. It really depends on your game design and without knowing more is hard to recommend an approach.

The Unity Multiplayer Services SDK has a session system that simplifies using Lobby and Relay together. You might consider checking that out. Also, an external developer recently created a really great walkthrough of some capabilities you will find useful to prototype, check it out here:

It’s our first time using Unity, and actually our first time developing a game. We haven’t tried anything yet and don’t know where to start.
I’ll check the Unity’s SDK and the video you sent.

Ok, gotcha, thanks for the details. Typically I’d recommend you try to make a few very basic single player experiences before attempting multiplayer. So for Unity in general there is a ton of free training content here: https://learn.unity.com/

After getting some initial experience with small prototypes you could explore multiplayer games and there are many frameworks/products that make this easier. One of the Unity supported solutions is documented here: About Netcode for GameObjects | Unity Multiplayer

Another external developer has a number of free courses on their youtube channel that are helpful, but below is a recent relevant one.

Between the Unity Learn website, the Netcode for Game Objects documentation, and these external developer youtube channels you’ll get a pretty rapid intro to making games in Unity. Good luck!

1 Like

In that case, unless you have a strong background in programming including distributed services and asynchronous tasks, I would strongly recommend to stay clear of online multiplayer game development for the time being simply because it’s several times harder even for experienced developers.

Create a singleplayer game first, perhaps with local multiplayer because that’s relatively easy to add in but already forces you to think of having “multiple” players and how to structure your game around those “multiples”. That’ll be hard enough to wrap your head around.

If you were to add networking (ie latency, ownership, authority, prediction, interpolation, reconciliation, matchmaking, lobby, …) onto your first game project you’ll likely be in way over your head and will likely have a rather frustrating experience because nothing will work as expected (particularly not physics) and you can’t compare what’s going wrong with any previous singleplayer gamedev experience either.

More starter’s advice from the top of my head:

  • don’t script camera behaviour, use Cinemachine
  • don’t write your own character controller, use one of the many existing solutions
  • disable domain reload to enter playmode instantaneously, and to force yourself to avoid static fields (including limiting the number of singletons)
  • browse Unity’s ebooks collection for topics of interest, these contain invaluable lessons
  • avoid using tutorials or buying assets (except art/audio) that are older than / have not been updated in the past two years
  • use source control to share your project - do NOT use any form of cloud synched folders to share projects since those destroy projects (cloud sync just overwrites changed files and typically the moment someone makes a change)
  • avoid building systems around coroutines - they have their uses but they’re also hard to debug and control (for instance, there is no way to check which coroutines are currently running unless you manually keep a record)
  • speaking of debugging: use the debugger when you’re stuck
1 Like

Ok, thank you so much.
We will check this.

Hi
We don’t have the choice to do it or not.
It’s in the bill of specifications.