Professional Documents
Culture Documents
Scratch
This document describes steps to setup a new multiplayer project from nothing using the new
networking system. This step-by-step process is generic, but can be customized for many types of
multiplayer games once it is started.
NetworkManager Setup
The first step is to create a NetworkManager in the project:
Add a new empty game object, from the menu Game Object -> Create Empty.
Find the newly created object in the Hierarchy View and select it
Rename the object to “NetworkManager” from the right-click context menu or by clicking on the
object’s name and typing.
In the inspector window for the object click the Add Component button
Find the component Network -> NetworkManager and add it to the object. This component manages
the network state of the game.
Find the component Network -> NetworkManagerHUD and add it to the object. This component
supplies a simple user interface in your game for controlling the network state.
Create a new Cube from the menu Game Object -> 3D Object -> Cube
Set the “Local Player Authority” checkbox on the NetworkIdentity to true. This will allow the client to
control the movement of the player object
Create a prefab from the player cube object by dragging it into the Assets window. This will create a
prefab called “PlayerCube”
Delete the PlayerCube object from the scene - we don’t need it now that we have a prefab
See Player Objects.
Find the NetworkManager object ain the Heirarchy View and select it
Open the “Spawn Info” foldout of the inspector for the NetworkManager
Find the “Player Prefab” slot
Drag the PlayerCube prefab into the “Player Prefab” slot
Now is a good time to save the project for the first time. From the menu File -> Save Project, save
the project. This will prompt for saving the current scene. Lets call this scene the “offline” scene.
using UnityEngine;
void Update()
var x = Input.GetAxis("Horizontal")*0.1f;
var z = Input.GetAxis("Vertical")*0.1f;
transform.Translate(x, 0, z);
This hooks up the cube to be controlled by the arrow keys or a controller pad.
Press “Host” to start the game as the host of the game. This will cause a player object to be created,
and the menu will change.
Pressing the arrow keys should make the player cube object move around.
using UnityEngine;
using UnityEngine.Networking;
void Update()
if (!isLocalPlayer)
return;
var x = Input.GetAxis("Horizontal")*0.1f;
var z = Input.GetAxis("Vertical")*0.1f;
transform.Translate(x, 0, z);
GetComponent<MeshRenderer>().material.color = Color.red;
This function is only called on the local player on their client. This will make that player see their
cube as red. This is a good place to do initialization that is only for the local player, such as
configuring cameras and input.
using UnityEngine.Networking;
GetComponent<MeshRenderer>().material.color = Color.red;
}
void Update()
if (!isLocalPlayer)
return;
var x = Input.GetAxis("Horizontal")*0.1f;
var z = Input.GetAxis("Vertical")*0.1f;
transform.Translate(x, 0, z);
if (Input.GetKeyDown(KeyCode.Space))
bulletPrefab,
transform.position - transform.forward,
Quaternion.identity);
bullet.GetComponent<Rigidbody>().velocity = -transform.forward*4;
Destroy(bullet, 2.0f);
using UnityEngine.Networking;
GetComponent<MeshRenderer>().material.color = Color.red;
[Command]
void CmdFire()
bulletPrefab,
transform.position - transform.forward,
Quaternion.identity);
bullet.GetComponent<Rigidbody>().velocity = -transform.forward*4;
NetworkServer.Spawn(bullet);
// when the bullet is destroyed on the server it wil automaticaly be destroyd on clients
Destroy(bullet, 2.0f);
void Update()
if (!isLocalPlayer)
return;
var x = Input.GetAxis("Horizontal")*0.1f;
var z = Input.GetAxis("Vertical")*0.1f;
transform.Translate(x, 0, z);
if (Input.GetKeyDown(KeyCode.Space))
CmdFire();
}
}
This code uses a [Command] to fire the bullet on the server. For more information see Networked
Actions.
if (hitPlayer != null)
Destroy(gameObject);
Now when a bullet hits a player it will be destroyed. When the bullet on the server is destroyed,
because it was spawned, it will be destroyed on clients too.
health -= amount;
if (health <= 0)
health = 0;
Debug.Log("Dead!");
The bullet script needs to be updated to call the TakeDamage function on a hit. * Open the bullet
script * in the collision handler function get the Combat component and call TakeDamage on it:
using UnityEngine;
if (hitPlayer != null)
combat.TakeDamage(10);
Destroy(gameObject);
This will make health on the player go down when hit by a bullet. But you cannot see this happening
in the game. We need to add a simple health bar. * Select the PlayerCube prefab * Choose the Add
Component button and add a new script called HealthBar * open the HealthBar script * this is a lot of
code that uses the old GUI system. This is not very relevant for networking so we’ll just use it without
explaination for now.
using UnityEngine;
using System.Collections;
GUIStyle healthStyle;
GUIStyle backStyle;
Combat combat;
void Awake()
{
combat = GetComponent<Combat>();
void OnGUI()
InitStyles();
GUI.color = Color.grey;
GUI.backgroundColor = Color.grey;
GUI.color = Color.green;
GUI.backgroundColor = Color.green;
void InitStyles()
}
if( backStyle == null )
pix[ i ] = col;
result.SetPixels( pix );
result.Apply();
return result;
using UnityEngine.Networking;
[SyncVar]
if (!isServer)
return;
health -= amount;
if (health <= 0)
health = 0;
Debug.Log("Dead!");
using UnityEngine.Networking;
[SyncVar]
if (!isServer)
return;
health -= amount;
if (health <= 0)
health = maxHealth;
RpcRespawn();
[ClientRpc]
void RpcRespawn()
if (isLocalPlayer)
transform.position = Vector3.zero;
Because in this game, the client controls the position of the player object, the server tells the client to
move the player object to the start position. If the server just set the player’s position to the start
position, it would be overridden by the client, since the client has authority.
using UnityEngine.Networking;
public class EnemySpawner : NetworkBehaviour {
Random.Range(-8.0f, 8.0f),
0.2f,
Random.Range(-8.0f, 8.0f)
);
NetworkServer.Spawn(enemy);
if (hitCombat != null)
hitCombat.TakeDamage(10);
Destroy(gameObject);
using UnityEngine.Networking;
[SyncVar]
if (!isServer)
return;
health -= amount;
if (health <= 0)
if (destroyOnDeath)
{
Destroy(gameObject);
else
health = maxHealth;
RpcRespawn();
[ClientRpc]
void RpcRespawn()
if (isLocalPlayer)
transform.position = Vector3.zero;