Unity3d_Multiplayer Netwoking17

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.

退出播放模式。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容