Documentation on how to make a multiplayer in the project
- Implement server creation and joining an existing host.
- Spawning as a player and how objects can be created on the network.
- Network communication using State Synchronization and Remote Procedure Calls.
- Interpolating and predicting values between data package
Creating a server
In the new Unity project, create a new C# script named “NetworkManager”. Add this empty script to an object in the scene, either the camera or an empty game object. It will handle hosting a server or connecting to an existing server.To create a server, we need to initialize it on the network and register it to the master server
in the created network script,one has to have the following code into the script
Joining a server
We now have the functionality to create a server, but cannot yet search for existing servers or join one of them. To achieve this, we need to send a request to the master server to get a list of HostData. This contains all data required to join a server. Once the host list is received, a message is sent to the game which triggers OnMasterServerEvent(). This function is called for several events, so we need to add a check to see if the message equals MasterServerEvent.HostListReceived. If this is the case, we can store the host list.To join a server, all we need is one entry of the host list. The OnConnectedToServer() is called after we actually joined the server.
private void JoinServer(HostData hostData)
{
Network.Connect(hostData);
}
void OnConnectedToServer()
{
Debug.Log("Server Joined");
}
if (!Network.isClient && !Network.isServer)
{
if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
StartServer();
if (GUI.Button(new Rect(100, 250, 250, 100), "Refresh Hosts"))
RefreshHostList();
if (hostList != null)
{
for (int i = 0; i < hostList.Length; i++)
{
if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList[i].gameName))
JoinServer(hostList[i]);
}
Spawning a player
Now we should be able to connect multiple players to one another, we can now extend the code with game mechanics. Set up a simple scene with a floor plane, player and some lighting. Add a rigid body to the player and freeze the rotations, so we will not get strange behaviour while moving.In the NetworkManager-script add a public game object variable for the player prefab. In the new function SpawnPlayer(), the prefab will be instantiated on the network, so all clients will see this object within their game. It requires a position, rotation and group, so I would suggest creating a spawn point.
The code in OnServerInitialized() and OnConnectedToServer() needs to be changed to spawn the player.
State Synchronization
Remote Procedure Calls
Another method of network communication is Remote Procedure Calls (RPCs), which is more useful for data that does not constantly change. A good example what we used these for in our Kickstarter demo is dialog. In this paragraph we will change the color of a player over the network.What RPCs do is a function call on a network view component and this component searches the correct RPC function. By adding [RPC] in front of the function, it can be called over the network. This approach is only able to send integers, floats, strings, networkViewIDs, vectors and quaternions. So not all parameters can be sent, but this can be solved. To send a game object, we should add a network view component to this object so we can use its networkViewID. To send a color, we should convert it to a vector or quaternion. An RPC is sent by calling networkView.RPC(), in which you define the function name and parameters. Also the RPC mode is required: “Server” sends the data to the server only,
An RPC is sent by calling networkView.RPC(), in which you define the function name and parameters. Also the RPC mode is required: “Server” sends the data to the server only.
REf:-
1 http://www.paladinstudios.com/2013/07/10/how-to-create-an-online-multiplayer-game-with-unity/
2. http://answers.unity3d.com/questions/537587/how-to-make-a-multiplayer-game.html
3. https://www.youtube.com/watch?v=AIgwZK151-A
4. https://doc.photonengine.com/en/pun/current/tutorials/photon-unity-and-networking-links