Use multiplayer networking to do a simple multiplayer example -1/2 (in another way)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.