SynMove.cs
usingUnityengine;usingSystem.Collections;usingunityengine.networking; Public classSynmove:networkbehaviour {//When the syncvar changes, Unet sends these changes from the Server side to all the valid Client side. Note that the direction here is from server to client, not from client to server. [Syncvar]PrivateVector3 Synpos; [Serializefield] Transform Mytransform; [Serializefield]floatLerprate = the; //Update is called once per frame voidfixedupdate () {transmitposition (); Lerpposition (); } voidlerpposition () {//not local players if(!Islocalplayer) { //update the location of non-local playersMytransform.position = Vector3.lerp (mytransform.position,synpos,time.deltatime*lerprate); }} [Command]//command, called on the client, but runs on the server, which is the method must start with CMD voidcmdprovidepositiontoserver (Vector3 pos) {//Assigning a value to a SYNPOS synchronization variable on the serverSynpos =POS; } [Clientcallback]//called only on the client voidtransmitposition () {//is a local player if(islocalplayer) {//send the local player's location to the serverCmdprovidepositiontoserver (mytransform.position); } }}
PlayerController.cs
usingUnityengine;usingSystem.Collections;usingunityengine.networking; Public classPlayercontroller:networkbehaviour {//Update is called once per frame voidUpdate () {//determine if you are a local player (you can only control your own players, you cannot control other people's players) if(Islocalplayer = =false) { return; } //gets the value of the horizontal axis of the keyboard floatH = Input.getaxis ("Horizontal"); //get the value of the keyboard vertical axis floatv = Input.getaxis ("Vertical"); //The protagonist moves aroundTransform. Translate (vector3.right*h*3*time.deltatime); //The protagonist moves forward and backwardTransform. Translate (vector3.forward*v*3*time.deltatime); }}
Use multiplayer networking to do a simple multiplayer example -1/2 (in another way)