美文网首页
Unity3d_Multiplayer Netwoking17

Unity3d_Multiplayer Netwoking17

作者: 慕城祉宇流年殇 | 来源:发表于2018-01-10 13:49 被阅读0次

    Spawning and Respawning

    产卵和重生

    Set Initial Spawn Positions for the Players

    为玩家设置初始的产卵位置。

    Currently the players are created and respawned at the origin point, or Vector3.zero.

    目前,玩家在原点或Vector3.zero中创建和响应。

    At the start of each game, unless we move one of the players before another joins, they will co-exist at the same point.

    在每一场比赛的开始,除非我们把其中一名球员移到另一组,否则他们将在同一点上共存。

    In an ideal situation, players should spawn at different locations.

    在理想情况下,玩家应该在不同的地点产卵。

    The NetworkStartPosition component can be used to do this, as it has a built-in feature for handling spawn positions.

    可以使用NetworkStartPosition组件来执行此操作,因为它具有用于处理派生位置的内置特性。

    To create two unique spawn positions we will need to create two new GameObjects, each with a NetworkStartPosition component attached.

    要创建两个独特的派生位置,我们需要创建两个新的GameObjects,每个都带有一个NetworkStartPosition组件。

    Create a new empty GameObject.

    创建一个新的空的GameObject。

    Rename the new GameObject “Spawn Position 1”.

    重命名新游戏对象“产卵位置1”。

    With the Spawn Position 1 GameObject selected,

    在被选中的子位置上,

    ...

    find and add the component Network > NetworkStartPosition.

    查找并添加组件网络> NetworkStartPosition。

    ...

    set the Transform Position to (3, 0, 0).

    将转换位置设置为(3,0,0)。

    Duplicate the Spawn Position 1 GameObject.

    复制生成位置1的游戏对象。

    Rename this duplicated GameObject “Spawn Position 2”.

    重命名这个复制的游戏对象“产卵位置2”。

    With the Spawn Position 2 GameObject selected,

    选择了产卵位置2,

    ...

    set the Transform Position to (-3, 0, 0).

    将转换位置设置为(- 3,0,0)。

    Select the Network Manager GameObject in the Hierarchy Window.

    在层次结构窗口中选择Network Manager GameObject。

    With the Network Manager GameObject selected,

    选择了Network Manager GameObject,

    ...

    open the Spawn Info foldout.

    打开生成的信息foldout。

    ...

    change the Player Spawn Method to Round Robin.

    将玩家衍生的方法更改为循环。

    Because the Spawn Position GameObjects have the NetworkStartPosition component on them, they will be found automatically by the NetworkManager.

    因为衍生的位置游戏对象在它们上有NetworkStartPosition组件,他们将被NetworkManager自动找到。

    The NetworkManager will then use the Transform Position of these GameObjects with NetworkStartPositions attached as locations for spawning the new player GameObjects when their Clients connect to the Server.

    然后,NetworkManager将使用这些游戏对象的转换位置,并将它们与networkstartposition连接起来,当它们的客户端连接到服务器时,它们将产生新玩家的游戏对象。

    There are two Player Spawn Methods: Random and Round Robin.

    有两种玩家衍生方法:随机和循环。

    As their name suggests, Random will use a random spawn point from the available NetworkSpawnPositions, and Round Robin will cycle through all of the available spawn positions before reusing them.

    顾名思义,Random将使用可用的networkspawnposition中的一个随机衍生点,轮询循环将在重用它们之前循环遍历所有可用的生成位置。

    With Random it is entirely possible that the same spawn position will be used by two or more player GameObjects, depending upon how many clients are connected.

    随机的,完全有可能相同的衍生位置将被两个或更多的玩家游戏对象使用,这取决于有多少客户端连接。

    With Round Robin, the spawn points will only be reused if there are more Clients than spawn points.

    对于轮询,如果有更多的客户机而不是派生点,那么派生点只会被重用。

    Test these changes:

    测试这些更改:

    Build and Run this scene as a standalone application.

    构建并运行这个场景作为一个独立的应用程序。

    Click the Host button from the in-game UI to start this game as a Host.

    单击游戏内UI中的主机按钮以作为主机启动此游戏。

    Return to Unity.

    回到Unity。

    Enter Play Mode.

    进入播放模式。

    Click the LAN Client button from the in-game UI to connect to the Host as a Client.

    单击游戏内UI中的LAN客户端按钮以连接到主机作为客户端。

    Now, when the game starts, the players are created at different positions in the scene.

    现在,当游戏开始时,玩家在场景中的不同位置创建。

    Close the standalone player.

    关闭独立的球员。

    Return to Unity.

    回到Unity。

    Exit Play Mode.

    退出播放模式。

    Set Respawn points

    设置刷新点

    The last and final step to this basic example is to create a simple system that uses the NetworkStartPosition component on the Spawn Position GameObjects to build an array of spawn points to choose from when the player is respawned.

    这个基本示例的最后一步和最后一步是创建一个简单的系统,该系统使用派生的位置游戏对象上的NetworkStartPosition组件来构建一个衍生点数组,以从玩家的响应中进行选择。

    This step is not strictly necessary for this Multiplayer Networking example, but makes the example feel a little more complete.

    这一步对于这个多玩家网络的例子来说并不是非常必要的,但是让这个例子感觉更加完整。

    It is worth noting that an even simpler way of saving the respawn location would be to save the local player’s position in the Start function as the respawn point.

    值得注意的是,一种更简单的挽救重生位置的方法是将本地玩家的位置保存在开始功能中作为重生点。

    This way the start position chosen by the Round Robin Player Spawn Method on the Network Manager would be “owned” by the player.

    这样,在网络管理器上由循环的Robin Player派生方法所选择的开始位置将被播放器“拥有”。

    To create a respawning system we will need to create an array, find all of the GameObjects with a NetworkStartPosition attached, add them to the array and use their Transform Position as a respawn point.

    要创建一个respawning系统,我们需要创建一个数组,找到所有带有NetworkStartPosition的GameObjects,将它们添加到数组中,并使用它们的转换位置作为重生点。

    This is very similar to what is being done automatically by the Network Manager for the start point, but in this simple case, we won't have a Round Robin solution, just the Random one.

    这与网络管理器为开始点自动完成的工作非常相似,但是在这个简单的例子中,我们不会有一个循环的Robin解决方案,只是随机的。

    Open the Health script for editing.

    打开用于编辑的健康脚本。

    Add a new array of the type NetworkStartPosition to hold the spawn points.

    添加一个新数组的类型NetworkStartPosition来保存衍生点。

    private NetworkStartPosition[] spawnPoints;

    Add a Start function.

    void Start ()

    {

    }

    In the Start function, add a check to test if this GameObject is associated with the Local Player.

    在启动函数中,添加一个检查来测试这个游戏对象是否与本地播放器相关联。

    if (isLocalPlayer)

    {

    }

    Add logic to find all instances of the NetworkStartPosition component and save them to the Spawn Points array.

    添加逻辑以查找NetworkStartPosition组件的所有实例,并将它们保存到派生点数组。

    spawnPoints = FindObjectsOfType();

    Note that this is the plural version: FindObjectsOfType.

    注意,这是复数版本:FindObjectsOfType。

    In the RpcRespawn function, inside the isLocalPlayer check, remove the existing code:

    在RpcRespawn函数中,在isLocalPlayer检查中,删除现有代码:

    // Set the player’s position to origin

    transform.position = Vector3.zero;

    In the RpcRespawn function, inside the isLocalPlayer check, update the code to use a random position from the array of spawn points.

    在RpcRespawn函数中,在isLocalPlayer检查中,更新代码以在衍生点数组中使用一个随机位置。

    // Set the spawn point to origin as a default value

    Vector3 spawnPoint = Vector3.zero;

    // If there is a spawn point array and the array is not empty, pick a spawn point at random

    if (spawnPoints != null && spawnPoints.Length > 0)

    {

        spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;

    }

    // Set the player’s position to the chosen spawn point

    transform.position = spawnPoint;

    The final script should look like this:

    Health

    C#

    using UnityEngine;

    using UnityEngine.UI;

    using UnityEngine.Networking;

    using System.Collections;

    public class Health : NetworkBehaviour 

    {

     public const int maxHealth = 100; 

     public bool destroyOnDeath;

     [SyncVar(hook = "OnChangeHealth")] 

     public int currentHealth = maxHealth; 

     public RectTransform healthBar; 

     private NetworkStartPosition[] spawnPoints;

     void Start () 

     { 

     if (isLocalPlayer) 

     {

     spawnPoints = FindObjectsOfType();

            }

        }

        public void TakeDamage(int amount)

        {

            if (!isServer)

                return;

            currentHealth -= amount;

            if (currentHealth <= 0)

            {

                if (destroyOnDeath)

                {

                    Destroy(gameObject);

                }

                else

                {

                    currentHealth = maxHealth;

                    // called on the Server, invoked on the Clients

                    RpcRespawn();

                }

            }

        }

        void OnChangeHealth (int currentHealth )

        {

            healthBar.sizeDelta = new Vector2(currentHealth , healthBar.sizeDelta.y);

        }

        [ClientRpc]

        void RpcRespawn()

        {

            if (isLocalPlayer)

            {

                // Set the spawn point to origin as a default value

                Vector3 spawnPoint = Vector3.zero;

                // If there is a spawn point array and the array is not empty, pick one at random

                if (spawnPoints != null && spawnPoints.Length > 0)

                {

                    spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position;

                }

                // Set the player’s position to the chosen spawn point

                transform.position = spawnPoint;

            }

        }

    }

    Save the script.

    保存脚本。

    Return to Unity.

    回到Unity。

    Test these changes:

    测试这些更改:

    Build and Run this scene as a standalone application.

    构建并运行这个场景作为一个独立的应用程序。

    Click the Host button from the in-game UI to start this game as a Host.

    单击游戏内UI中的主机按钮以作为主机启动此游戏。

    Return to Unity.

    回到Unity。

    Enter Play Mode.

    进入播放模式。

    Click the LAN Client button from the in-game UI to connect to the Host as a Client.

    单击游戏内UI中的LAN客户端按钮以连接到主机作为客户端。

    Now, when the game starts, the players are created at different positions in the scene.

    现在,当游戏开始时,玩家在场景中的不同位置创建。

    When a player’s current health drops to zero, the Player should respawn randomly at one of the existing spawn positions.

    当玩家当前的降为零时,玩家应该在现有的一个衍生位置上随机重生。

    Close the standalone player.

    关闭独立的球员。

    Return to Unity.

    回到Unity。

    Exit Play Mode.

    退出播放模式。

    相关文章

      网友评论

          本文标题:Unity3d_Multiplayer Netwoking17

          本文链接:https://www.haomeiwen.com/subject/iilonxtx.html