-
Notifications
You must be signed in to change notification settings - Fork 38
Networking how to
There is a gameObject in the level with a single script on it called "networkManager". This script starts everything off. When it loads you can either pick to host or join a game. When you pick, it will load up "networkManagerServer" or "networkManagerClient" and stop itself. Both of these then load any scripts which need networking information (for example which buggy is yours).
In "networkManager" you can set certain objects (for example a camera) as variables that can then be passed on to "networkManagerServer" or "networkManagerClient". You should only add variables to "networkManager" and not functions (so any gameObjects, the current build version, etc...).
In "networkManagerServer" and "networkManagerClient" there's a section where you can add custom scripts to be loaded with the variables you set in "networkManager". Both of these scripts are attached to the gameObject "networkManager" is running on, but will have a reference to the players cart and their NetworkViewID by use of the variables 'myCart' and 'myViewID'.
In "networkManager" you need to create a reference to a camera that will be used by the script that follows your buggy. You would add this line of code to the top:
public GameObject myCamera;
Then find these lines
` // add the server script to us
networkManagerServer nms = gameObject.AddComponent("networkManagerServer") as networkManagerServer;
// set anything that needs to be set `
and add underneath them
nms.myCamera = myCamera
This will transfer the camera reference to the server script. You also need to do the same thing for when the client script loads. Now in "networkManagerClient" you need to create a reference to the camera, so add the same line of code to the top of this script:
public GameObject myCamera;
Now find the line
// CLIENT SIDE SCRIPTS GO HERE
and add a reference to the script by using the following lines of code:
myScriptName someScript = gameObject.AddComponent("myScriptName") as myScriptName;
and set the camera on it with
someScript.myCamera = myCamera;
You will also want to pass on the reference to the players buggy, and to do that you use
someScript.myCart = myCart;
This will now load up "myScriptName" when you choose to join a game (ie play as a client) and will give the script a reference to both the camera you attached to "networkManager" in the inspector menu, and the players cart. The way to do it for the "networkManagerClient" script is almost identical, but make sure you do it for both if you want the host to have a moving camera!