-
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.
Network connections are managed from a Unity Master Server instance, with the Master Server IP defined in networkManager.Start() in networkManager.cs (currently pointing at a server hosted by bobsayshilol).
Client<->Client interaction should never be used! Make sure everything goes through the server and is then forwarded on to the other clients.
All information on a player is kept in a class called PlayerInfo
. This contains the location's, the NetworkViewID's, and the model's of the player's cart, character, and ball. Here's a run down of what it stores:
- This is a direct reference to a player's NetworkPlayer object and is present in any RPC call from a client so you can see who if was from
- The main use of this is to easily look-up the
PlayerInfo
when you receive an RPC - http://docs.unity3d.com/Documentation/ScriptReference/NetworkPlayer.html
- This is a direct reference to the server's NetworkPlayer object
- The main use of this is to check your ping
- If you are hosting then
server
andplayer
will be the same - http://docs.unity3d.com/Documentation/ScriptReference/NetworkPlayer.html
- This is the NetworkViewID of the transform of a players cart
- Not really used outside of the
networkManager
's - https://docs.unity3d.com/Documentation/ScriptReference/NetworkViewID.html
- This is the NetworkViewID of the rigidbody of a players cart
- Not really used outside of the
networkManager
's - https://docs.unity3d.com/Documentation/ScriptReference/NetworkViewID.html
- This is a reference to the GameObject of a players cart
- From this you can the location, velocity, etc...
- When setting location, velocity, etc... the server will have the final word on where it's meant to be - ie if a client changes it's velocity but doesn't tell the server to then it'll keep rubber-banding back to where the server thinks it should be
- This is the model of the cart
- This is the NetworkViewID of the transform of a players ball
- Not really used outside of the
networkManager
's - https://docs.unity3d.com/Documentation/ScriptReference/NetworkViewID.html
- This is a reference to the GameObject of a players ball
- From this you can the location, velocity, etc...
- When setting location, velocity, etc... the server will have the final word on where it's meant to be - ie if a client changes it's velocity but doesn't tell the server to then it'll keep rubber-banding back to where the server thinks it should be
- This is the model of the ball
- This is the NetworkViewID of the transform of a players character
- Not really used outside of the
networkManager
's - https://docs.unity3d.com/Documentation/ScriptReference/NetworkViewID.html
- This is a reference to the GameObject of a players character
- From this you can the location, velocity, etc...
- When setting location, velocity, etc... the server will have the final word on where it's meant to be - ie if a client changes it's velocity but doesn't tell the server to then it'll keep rubber-banding back to where the server thinks it should be
- This is the model of the character
- This is the current mode of a player
- Current values are:
- 0 - in buggy
- 1 - at ball
- 2 - spectate
- This is the name of the player
- player is engaged in an uninteruptable action
- Isn't actually synced yet
- player is paused
- Isn't actually synced yet
- player accelleration/brake input
- Used by
controlServer
andcontrolClient
- player steering input
- Used by
controlServer
andcontrolClient
All networked information is kept in the networkVariables
script. To access this use
networkVariables nvs = GetComponent("networkVariables") as networkVariables;
nvs
can then be used to get your PlayerInfo
. Here's a run down of what it stores:
- This is a direct reference to the current players
PlayerInfo
- The main use of this is to get the location of the players cart, ball and character
- This is the internal server version
- If you ever add/remove/edit RPC calls then you'll want to change this as it will break if people join who don't have those calls
- Current version: sandvich3
- This is the name of the server
- This is an ArrayList of all the players currently on the server
- Both servers and clients have access to this list
- The main use of this is to find a certain player's cart by iterating through it
- This is a reference to the camera in a scene
- This needs setting in the inspector view for each level
- This is the height at which players carts are reset back to their initial location
- This needs setting in the inspector view for each level
- This is the list of models that can be used as buggys as they appear in the resources folder
- This is the list of models that can be used as buggys as they appear to the player (ie their nice name)
- This is the list of models that can be used as balls as they appear in the resources folder
- This is the list of models that can be used as balls as they appear to the player (ie their nice name)
- This is the list of models that can be used as characters as they appear in the resources folder
- This is the list of models that can be used as characters as they appear to the player (ie their nice name)
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!
To disconnect you simply call 'Network.Disconnect()' and the server will clean up anything you owned from everyone else.