Skip to content
bobsayshilol edited this page Mar 31, 2014 · 19 revisions

How it works:

need to update this...

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.

Framework usage:

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:

PlayerInfo

player

server

cartViewIDTransform

cartViewIDRigidbody

cartGameObject

  • 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

cartModel

  • This is the model of the cart

ballViewID

ballGameObject

  • 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

ballModel

  • This is the model of the ball

characterViewID

characterGameObject

  • 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

characterModel

  • This is the model of the character

currentMode

  • This is the current mode of a player
  • Current values are:
  • 0 - in buggy
  • 1 - at ball
  • 2 - spectate

name

  • This is the name of the player

playerIsBusy

  • player is engaged in an uninteruptable action
  • Isn't actually synced yet

playerIsPaused

  • player is paused
  • Isn't actually synced yet

v

  • player accelleration/brake input
  • Used by controlServer and controlClient

h

  • player steering input
  • Used by controlServer and controlClient

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:

networkVariables

myInfo

  • 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

serverVersion

  • 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

serverName

  • This is the name of the server

players

  • 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

myCam

  • This is a reference to the camera in a scene
  • This needs setting in the inspector view for each level

lowestHeight

  • 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

buggyModels

  • This is the list of models that can be used as buggys as they appear in the resources folder

buggyModelNames

  • This is the list of models that can be used as buggys as they appear to the player (ie their nice name)

ballModels

  • This is the list of models that can be used as balls as they appear in the resources folder

ballModelNames

  • This is the list of models that can be used as balls as they appear to the player (ie their nice name)

characterModels

  • This is the list of models that can be used as characters as they appear in the resources folder

characterModelNames

  • This is the list of models that can be used as characters as they appear to the player (ie their nice name)

Basic example - camera following script:

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.