Unity Official Website Tutorial--Multiplayer Networking

Source: Internet
Author: User

Tutorial URL: https://unity3d.com/cn/learn/tutorials/topics/multiplayer-networking/ introduction-simple-multiplayer-example?playlist=29690

1. Create a new 3D project, in the menu "File"-"Save Scenes", save the scene as "Main". Note that the saved files are placed in the assets directory.

2. Menu "Gameobject"--"Create Empty", create a new Gameobject, right-click Rename to "NetworkManager".

3. In the Hierarchy panel, select NetworkManager. Then, in the Inspector panel, click on the "Add Component" button to search for the addition of two components: NetworkManager and Networkmanagerhud.

The NetworkManager component manages the network status of the game.

The Networkmanagerhud component works with NetworkManager and provides a simple user interface to control the network state of the game when it is running.

4. Menu "Gameobject"-"3D object", create two new objects: "Cube" and "Capsule". In the "Hierarchy" panel, drag "cube" to "Capsule", Capsule as the parent component of the cube. Rename Capsule and Cube respectively to "Player" and "Visor". This object, as a substitute for a game character.

5. In the hierarchy panel, select Visor. Then, on the Transform tab of the Inspector panel, change the two properties: position and scale.

    • set the visor  gameobject ' s scale to (0.95, 0.25, 0.5).
    • set the visor  gameobject ' s Position to: (0.0 , 0.5, 0.24)

6. To make the player mimic a game character more vividly, you need to change the color of the visor. In the project panel, right-click "Create"--"Material", create a new Material object and name it "Black". With the Black object selected, in the main maps of the Inspector panel, change the albedo color to dark. Drag the material to the visor object on the hierarchy panel.

7. Give the game character in the network an ID card. Select the player for the hierarchy panel, and then select the menu "Component"--"NetworkManager"--"networkidentity". Next, in the Inspector panel, select "Local Player Authority" in Network identity. This allows the client to control the behavior of the player.

8. Drag the Player object in the hierarchy panel to the project panel, and create a pre-made object for the player , called the player prefab. You can then delete the Player object in the hierarchy panel.

9. Save it for a moment.

10. Select the NetworkManager object for the hierarchy panel, expand the Spawn Info option for the Inspector panel, and have a platyer Prefab. Drag the assets player prefab into the player prefab. (Official website means: There should be a lot of games are only one game protagonist controlled by the player, so, when the client connected to the server, here will generate a corresponding game object, easy to manage.

11.Assets panel, create a new player directory, right-click the directory "Create"--"C # Script", name the new file as "Playercontroller", open, copy the following code to the file. Use this script to control the behavior of game characters.

1 usingUnityengine;2 3  Public classPlayercontroller:monobehaviour4 {5     voidUpdate ()6     {7         varx = Input.getaxis ("Horizontal") * Time.deltatime *150.0f;8         varz = Input.getaxis ("Vertical") * Time.deltatime *3.0f;9 TenTransform. Rotate (0X0); OneTransform. Translate (0,0, z); A     } -}
View Code

by Default ,   input.getaxis (" horizontal ")  AND  input.getaxis (" Vertical ")  allow the player to use the WASD and arrow keys, a controller pad or other device to move the PLA Yer. For more information, please see the page on the  Input Manager.

12. Select the NetworkManager object in the hierarchy panel and drag the new script to the "Add Component" button at the bottom of the inspector.

13. Return to unity and save the scene.

14. Click the upper triangle to run the button. After running, click "LAN Host (H)", then you can press "AWSD", or the arrow keys to control the movement of the object.

LAN Host (H): Client and Server run together

LAN Client (C): Only the client mode

LAN server: only server mode.

16. Run the program in a separate window. "File"-"Building Settings ..."

Pop-up window save executable file

17. Now there are two programs to run, one is the Unity Emulator, a separate window to run, you can choose which as the server, which as the client.

18. Note that the behavior of multiple clients is not synchronized because Playercontroller does not have the relevant code and can develop itself as a learning.

Unity Official Website Tutorial--Multiplayer Networking

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.