Client served multiplayer in Unity (2D)

I know that this question may seem a little bit.. um… Big or off-topic, but please understand me: I’m a 12 year old young programmer that tries to do his best.

I would want to make a multiplayer game in Unity, but the server is the host’s device ( if it sounds familiar to you, yeah it is inspired by the Minecraft multiplayer with 8 people).

Soo, I want someone to host the game (like someone opens the game and his/her device becomes a server). How can I do this?

P.S.: The game will be 2D and practically lightweight (it will have a maximum of 2 players), so it won’t consume a lot of resources.

P.S. 2: I would want the game to have something like a unique code or something for the other player to join.

P.S. 3: I will write the game in C#.

Thank you in advance! 😉

Answer

that’s a cool project you have there and wish you some success. There are many ways to do what you are asking for.

For example the person who will host the server can share some code to other players(which when decrypted contains the IP address to connect to), or directly the IP address. For example MTI3LjAuMC4xOjIwMDAK is base64 encoding for 127.0.0.1:2000 using only the following lines of code

//Encode in base 64
public static string Base64Encode(string plainText) {
  var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
  return System.Convert.ToBase64String(plainTextBytes);
}

More advanced would be you have two servers one for the player to notify that he is hosting a server, and that server would be listed and visible to his friends from the main server which is just listing and checking who can access which servers based on a friend list for example.

and so on….

If it is for a simple project I would probably go with option 1.

Leave a Reply

Your email address will not be published. Required fields are marked *