09 Sept 2014Unity15 Comments

Unity3D Networking

In this section, i’ll explain fundamentals of unity networking, Unity’s networking functionality is very simple, but its always good to understand the best practices before you create multiplayer game.

Consider this is like a chat room. Anyone can create new room, room has unique name and maximum clients. people can join existing rooms. Unity networking also like that, players can create a server by providing, unique game name and server name. once a server created it should registered to the master server, unity user master server is use to match making between servers and clients. this master server service is provided by unity, if you want you can create your own master server. By using Network.InitializeServer() method we can easily create a host. Number of allowed incoming connections, listen port, enable or disable NAT punchthrough functionality should pass as parameters Once server has successfully created, OnserverInitialized() will called.

Now our created server is up and running, we want a client to join. As i mentioned before, master server has all the data regarding the live servers. Client’s first step should be request all the live servers Initialized as given game name. in this case our game name will be “UnityNetworkingPrototype”. all the available hosts received as HostData array

Let’s connect to any of the host by Network.Connect();

After successfully joined to any of the servers, OnConnectedToServer will fired

so far so good.Now you know how to create server, register it and to the master server and join existing server as client. Lets add a player object to our prototype. Lets add a player to the scene (GameObject > create other > Cube). Our next challenge will be synchronising players’ data (speed, position, rotation etc ) over the network. This can be done easily by using unity’s network view component. This is kind of binding material for multiplayer games. With this you can define exactly what is to be synchronized over the network and how it should be done.

Add network view component to the player (Component > Miscellaneous > NetworkView). Create new C# script and rename it as Player. Then add basic navigation methods to navigate it using arrow keys.

Basic navigation part is done, now we want to spawn our player object over the network. Network.Instantiate () method will instantiate our player on the network. all the room members will see this within their game. Synchronization is automatically set up so there is no extra work involved. The position, rotation and network group number are given as parameters.

Who can control the player?

We should allow users to control their local players only, now all the players will get our input controls so we need to check the owner of the network view before sending input controls.

Synchronising positions

By default NetworkView’s observed component is set to Player Transform. that means player position, rotation will be synchronized throught the network. Some times its better to have more control to data which are exchange over the network. NetworkView’s OnSerializeNetworkView() method enable us to define which variables are sent or received for synchronise player position. This function is automatically called every time it either sends or receives data.

Up to this point all the functions are working fine but, there is a lag between two players. for reduce this, we can use interpolation

Further optimisation . . .

Based on the network conditions there will be a small delay, for reduce that small delay we can predict our players next position based on the current velocity.

Download complete unity project

Tags: