Building a simple online game server for Unity

I am trying to create an online game server for my 2D tank game (Unity). In my game, 2-4 players will control their tanks and fight each other.

  • I tried to use Unity networking, it was not very suitable for my game, because we have to select one of the players in the room to become a "server", which is not very flexible for my future (for example, when the "server" shuts down, I you have to work hard to stay connected with other players).

  • Then I tried to create my own server with Nodejs lΓ  socket.io for communication between server and client. It's very simple: it receives data from one and broadcasts it to others. It seems to work fine until the physical part comes along: the server has to trust the clients when they say that something got hit or exploded and then relayed it to other clients. Not to mention cheated clients, with network latency, the physical simulation of clients will be different. For example, a tank can be hit by this client, but it must be closed behind a wall on another and stay alive, but the tank behind it catches a bullet and explodes due to latency. In these cases, the server does not know which one to listen to.

Update

  • As @Catwood mentioned below, Photon PUN is another option for me. I previously followed one of my tutorials. Photon doesn't need the player to be a "server" like Unity networking. But how do I implement my game logic on the server? (for an authoritative server)
  • Apparently Smartfoxserver is another good (and expensive) option. I didn't go deeper into their API document. I don't know if I can implement my game logic and rules on this one (their tutorial will skip this part for simplicity).

In conclusion, I need a suggestion for my game server:

  • Is an authoritative server
  • Can handle the rules of the game and decide what happens (perhaps needs to have its own physics engine).
  • Works well with Unity2D
  • Javascript, C #, Java are preferred!

I'm going in the right direction because it seems that some game server services (like Photon, Unity networking) just don't care about how to implement the game logic on the server. And does it make them a non-authoritative server?

I am very new to this field, everything would be appreciated!

+3


source to share


2 answers


I would recommend creating your own server for your game, just like in NodeJS. Most of the solutions I know are quite difficult to use and can't do everything you want.

For example, Blizzard's Hearthstone game is based on Unity for the client and has a custom server side.

Here are some tips on how to create a game server.

Everything seems to be fine until the physical part comes in: the server has to trust the clients when they say something got hit or exploded and then relayed it to other clients.

When creating a server, you should make all important decisions on the server side, not the client side. This is the server that starts the party, so the server should have the following information

  • Map size
  • Number of players
  • Data about each player (health point, position, size, attack speed ...)
  • Any other information necessary for making decisions.

Now the client should always send a very small signal to the server as shown below.

  • Move left
  • Join the group
  • Shoot
  • Turn right
  • etc...

Based on user actions and user data, the server can run a "simulation" of the game.

  • Check if the action is allowed.
  • If allowed, simulate the action. If it doesn't, put it in the queue
  • Send users information about what's going on (player one hits, player two dies, player four moves to the left, etc.)

In doing so, the server knows when something is happening and decide what will happen. You don't rely on client-side information, you only get the desired player actions.

However, as you said, due to latency and other network factors, your client may not be too dependent on the server. In modern games, the client has the same player information as the server and does not always rely on the server to display what is happening on the screen.

If you have played some online games, you may have noticed that when the connection to the server is lost, there is a small amount of time during which you can continue to play (move, shoot, etc.), but nothing moves except you. This is because the client continues to "launch" the game based on your action, even without the server information.

However, to avoid a huge difference between what the client displays to the player and what happens in the server simulation, the client and server are "synchronized" at regular intervals.

For example, if you decide to move left, the client knows your speed so it can display the movement without relying on the server.



When synchronization occurs, the server sends critical information to the client, and the client changes any currently displayed information with what the server is sending.

In the left movement example, if your movement speed is different on the server and on the client when the client receives a sync order, you will notice that your player will be "teleported" from the displayed position to another. It can also happen if a packet is lost or due to high latency.

Lantency handling is a huge issue when creating an online game on both the server and client side and is not the subject of this question.

To summarize, your server should

  • be homemade
  • Only receive actions from clients
  • Simulate the game
  • Send clients information about what's going on.
  • Synchronization at regular intervals with clients

Hope this help =)


Here are some explanations on how to add logic inside your server. Slight disclaimer before, I've never used NodeJS so I don't know if this is possible with NodeJS, I usually use C ++.

Now for your game, I am assuming that the player can only use the MOVE action.

When a user connects to your server, you can launch the game. Since your user can move, this means there is a 2D map that your user has in size and start position and speed. So your server should start a new "GameParty" and initialize the above data. For example, let's say the default is set.

map_width = 512;
map_height = 512;
user_width = 2;
user_height = 2;
user_speed = 1;
user_posx = 20;  
user_posy = 20;

      

When a client wants to MOVE, he sends a packet to the server saying he wants to move. You can use whatever protocol you want to communicate with client and server, I am using binary protonal but let's say you are using Json

{action: move; value: left};

      

This will make your server know that the user wants to navigate to the left. So you need to decrease user_posx

by a value user_speed

to have the server side, your new position. If this position is on the edge of the map, you have two options: the user can appear on the other edge of the map to prevent the action.

At regular intervals, your server will send the client the player's current position.

+7


source


The asset store has an asset called Photon , which is an authoritative server. It is also free for non-mobile devices. This will surely handle the first part of your question.



This excellent tutorial will help.

+1


source







All Articles