Authoriative Server Demo

A small example project to demonstrate a minimal authoritative server. There are two scenes in the project, one for the server and one for the clients.

Each player controls one animated "Lerpz" character. The server has no character, but it has a camera to ease debugging. Player movement is sent to the server using an RPC called "Move". The server, in turn, sends proxy state syncs including position, rotation and velocity information to the opponents so that all players can see each other's movements. To minimize server load, there are no animations on the server.


The project also includes a chat GUI that is designed to be used in an authoritative server. The chat messages are sent as RPCs and they are broadcast by the server without any security or language checks, but it is easy to add such checks to the code.

Open the project

  1. Download the file and unpack it
  2. In the Unity editor, open the "LerpzAuthServer" project folder that was unpacked by the downloaded file.

Run and test on a single machine

  1. Double-click the "Server" scene.
  2. Build and run the scene as a "PC and Mac Standalone". Shortcut key is Ctrl+Shift+B.
  3. In the started window the server can see what is happening (The camera can't move).
  4. Double-click the "Client" scene and then play that scene in the editor.
  5. In the game window, press "Play on Localhost" button.
  6. Place the windows beside each other, client and server.
  7. Now you should be able to test the game and walk around with the Lerpz character.
  8. Verify that movements, but not animations, are replicated in the server window.
  9. Build and run serveral stand-alone clients to do more testing. Verify that animations do work in all clients.

Run and test on two different machines

  1. Double-click the "Server" scene.
  2. Build the scene as a "PC and Mac Standalone". Shortcut key is Ctrl+Shift+B.
  3. Transfer the server exe file + Data folder to another computer. (Mac users: transfer the app file)
  4. Start the server at the other computer by clicking the exe/app file.
  5. In the started window the server can see what is happening.
  6. Double-click the "Client" scene and then play that scene in the editor.
  7. In the game window, press "Advanced" button. You should be able to find the running server on the LAN page. If you can not find it on the LAN page, please disable any firewall like Windows firewall momentarily just to make sure it is not blocking.
  8. Now you should be able to test the game and walk around with the Lerpz character.
  9. Verify that movements, but not animations, are replicated in the server window.
  10. Build and run serveral stand-alone clients to do more testing. Verify that animations do work in all clients.

Examine the code

This code is written to be a short and a dedicated demo for a minimal authoritative server. Use it as an example to get started with your own game. Below are some important things to look at.

General setup

  • Notice that the checkbox "Authoritate Server" is marked in the editor menu: uLink -> Edit Settings...

Prefabs

  • There are three important prefabs in the project, LerpzCreator, LerpzOwner and LerpzProxy. All of them are used in the script AuthSpawnPrefab.cs when a player connects to the server. Only the server can do uLink.Network.Instantiate() in an authoritative setup, clients are incapable of doing this to avoid cheating.
  • LerpzOwner is the prefab instantiated on the client for the player's character. Motion of this object is controlled by the player. It is connected to the camera in the "Client" scene. The OwnerInit.cs script makes the camera connection.
  • LerpzCreator is the prefab instantiated on the server for every player connecting to the game. It has fewer scritps than LerpzOwner. The server has, for example, no script for activating animations, since this is not important on the server and would only add extra CPU load. If you want to see animations on the server, just add the script ThirdPersonSimpleAnimation.cs to LerpzCreator.
  • The prefab LerpzProxy is instantiated in clients for all the opponent players' characters. This prefab has one extra script attached to it, uLinkObjectLabel.cs. It activates a nametag above the character. This way you can see the name of you opponents.

Smooth synchronization of movements

  • The observed property of the uLinkNetworkView of all three Lerpz prefabs are set to uLinkSmoothCharacter. This is a utility script that is very handy. It works in both authoritative servers and non-authoritave servers. It makes the movement of opponents' avatars smooth.
  • An RPC called "Move" is sent from the LerpzOwner in the client to the LerpzCreator in the server. The code for sending this RPC and receiving this RPC is in the script uLinkSmoothCharacter.cs.
  • There is no state sync traffic from clients to the server. This is forbidden in an authoritative server. The statesync sent from the server is also implemented in uLinkSmoothCharacter.cs.
  • LerpsProxy receives statesyncs sent from the server. Once again the code for this is located in uLinkSmoothCharacter.cs
  • Animation state is NOT sent over the network, only position, rotation and velocity. This saves bandwidth. The correct animation state is calculated in every client based on the object's velocity. The calculation is done in the script ThirdPersonSimpleAnimation.js.
  • The serialization of position, rotation and velocity could be optimized in several ways to minimize bandwidth, but it has not been done in this simple demo.

Miscellaneous

  • The script uLinkSimpleServer.cs detects player disconnects and runs the code in uLink_OnPlayerDisconnected(). This code removes a player's character from all other clients and also from the server, it also removes the uLink "Instantiate RPC" for that player from the RPC buffer.

Unity web player as client

Make sure you are running on the same host as the game server. The policy server is needed to be able to connect to the game server from a Unity web player.