Unity3d unet mimic LAN Game (iii)

Source: Internet
Author: User

Continue on the blog, we can already launch bullets and let the character get hurt. Now we create some "cannon fodder". Use the player to modify the appearance,

Named enemy, made into prefab. is not a little punk feeling.
Create a new empty object in the scene and rename it to Enemyspawn as cannon fodder incubator, used to produce cannon fodder. Now we produce cannon fodder, this cannon fodder needs to be generated on the server side, and synchronized to the client side. Modify Network Manager

To add the network identity component to Enemyspawn, select Server only to indicate that only the service side has permissions. Add the Enemyspawn script to him and edit it:

usingUnityengine;usingSystem.Collections;usingunityengine.networking; Public classEnemyspawn:networkbehaviour { PublicGameobject Enemypre;//used to get cannon fodder this object     Public intEnemycount =6;//Define the number of cannon fodder to be generated    //networkbehaviour built-in method that is called when the server is started     Public Override void Onstartserver()    { for(inti =0; i < Enemycount; i + +) {//Give the cannon fodder to be produced a random position, the height unchangedVector3 eposition =NewVector3 (Random.range (-8F8f),0, Random.range (-8F8f));//Give him a direction, that is, rotation around the y-axis, other unchanged            //quaternion.euler: Turn a rotation into four elementsquaternion erotation = Quaternion.euler (0, Random.range (0F thef),0);//instantiation of this cannon fodderGameobject enemy = Instantiate (Enemypre, Eposition, erotation) asGameobject; Networkserver.spawn (enemy);//Generate an object on each client}    }}

Effect:

If you can't see clearly, you can change the location of the camera.
Now it's working, but it's not going to go away, and now it's going to disappear. Edit the Health class, here to modify the code is relatively small, I directly map, we modify the corresponding place on the line:

Note: You need to check enemy this prefab tick Destroyondeath

Now it's time to destroy cannon fodder.

Finally, we add two empty objects in the scene, not the same position, separate a bit. Name: Network start Position1 and network start Position2. Add the network start position component to them. Because they have this component, Network Manager will get these two things. Modify Network Manager

is next to each of these two positions generated, and now control the character rebirth at random at these two points in the position, modify the health class:

usingUnityengine;usingSystem.Collections;usingUnityengine.ui;usingunityengine.networking; Public classHealth:networkbehaviour { Public Const intMaxHealth = -;//Maximum blood volume    //detects a property that is synchronized to the client when the server changes the value    //When the value changes, a method is called such as: Onchangehealth[Syncvar (Hook ="Onchangehealth")] Public intCurrenthealth = MaxHealth;//Current blood volume     PublicSlider Healthslider; Public BOOLDestroyondeath =false;//used to determine if the object should be destroyed    PrivateNetworkstartposition[] spawnpoints;//Get this networkstartposition component    voidStart () {if(Islocalplayer)//If the client{spawnpoints = findobjectsoftype<networkstartposition> (); }    }//method to be called when player and bullet collide     Public void Takedamage(intDamage) {if(!isserver)//If not the server{return; } currenthealth-= damage;if(Currenthealth <=0)//If the current amount of blood is less than or equal to 0{if(Destroyondeath)//Determine if the object can be destroyed{Destroy (gameobject);return; } currenthealth = MaxHealth;//Let him return to the full blood state, convenient to the back we let him reborn when neededRpcrespawn ();//Call a method to set the spawn location}    }//method called when the value of the property detected by the server changes    //health: The changed value    voidOnchangehealth (intHealth) {Healthslider.value= (float) Health/maxhealth;//Modify the scale of the slider}//Remote Call, indicating that the method is called at the client, and the method name must start with RPC[CLIENTRPC]voidRpcrespawn () {if(!islocalplayer) {return; } Vector3 spawnposition = Vector3.zero;if(Spawnpoints! =NULL&& spawnpoints.length >0)        {//Random get networkstartposition in the array of one, in getting its positionSpawnposition = Spawnpoints[random.range (0, spawnpoints.length)].transform.position;      } transform.position = Spawnposition; }}

Now you can get the results. When a role is generated, it is generated at two points on the set, and when it is reborn, it is randomly generated at two points.
Well, this blog ends here.
File project has been uploaded: http://pan.baidu.com/s/1pKSGZG3 password: C4CR
This blog is used as a beginner to study the discussion, if there are bad or wrong place also please point out, thank you.

Unity3d unet mimic LAN Game (iii)

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.