-
Notifications
You must be signed in to change notification settings - Fork 38
Networking how to
There is a gameObject in the level with 2 scripts on it: one called "networkManager" and another called "networkVariables". "networkManager" 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 "networkVariables" you can set certain objects (for example a camera) as variables that can then be passed on to and other networked scripts that need them. You should only add variables to "networkVariables" and not functions (so any gameObjects, the current build version, etc...).
In both "networkManagerServer" and "networkManagerClient" there's a section where you can add custom scripts to be loaded. Both of these scripts are attached to the same gameObject "networkManager" and "networkVariables" are running on and will load the scripts onto this gameObject aswell.
In "networkVariables" you need to create a reference to a camera that will be used by the script that follows your buggy. To do this you add this line of code to the top:
public GameObject myCamera;
and then in the inspector in Unity you can point it to the camera you want to.
In your script you will probably have that same line at the top already. To set the reference you need to add this to the 'Start' function:
// get variables we need networkVariables nvs = GetComponent("networkVariables") as networkVariables; camera = nvs.myCamera;
This finds the script that has the variables on and sets our variable camera
to the one we specified in "networkVariables". You can add as many variables as you want to "networkVariables", but make sure that it doesn't interfere with someone else's script.
Now in "networkManagerClient" you need to add the script so that it gets loaded. Do this by finding the line:
// CLIENT SIDE SCRIPTS GO HERE
and adding
gameObject.AddComponent("myScriptName");
This will now load up "myScriptName" when you choose to join a game (ie play as a client). The way to do it for the "networkManagerServer" script is almost identical, but make sure you do it for both if you want the host to have a moving camera!