Unity3d_Multiplayer Netwoking14

Death and Respawning

死亡和重生

Currently, nothing happens to the player when the player’s current health reaches zero except for a message in the console on the Server.

目前,除了服务器上的控制台的消息之外,播放器当前的健康状态为0时,播放器没有发生任何变化。

To make this current example behave more like a game, when the player’s current health reaches zero the player will be teleported back to the starting location with full health.

为了使当前的例子更像一个游戏,当玩家的当前健康达到零的时候,玩家将被传送回起始位置,并拥有完整的健康状态。

This will also serve as a way to introduce the [ClientRpc] attribute, which is another tool for State Synchronization.

这也将成为引入[ClientRpc]属性的一种方式,这是另一个用于状态同步的工具。

ClientRpc calls can be sent from any spawned object on the Server with a NetworkIdentity.

ClientRpc调用可以从服务器上的任何衍生对象发送到一个NetworkIdentity。

Even though this function is called on the Server, it will be executed on the Clients.

即使这个函数在服务器上调用,它也会在客户机上执行。

ClientRpc's are the opposite of Commands.

ClientRpc的功能与命令相反。

Commands are called on the Client, but executed on the Server.

命令在客户机上调用,但在服务器上执行。

ClientRpc's are called on the Server, but executed on the Client.

在服务器上调用ClientRpc,但在客户机上执行。

To make a function into a ClientRpc call, use the [ClientRpc] attribute and add “Rpc” as a prefix to the name of the function.

要使一个函数成为一个ClientRpc调用,可以使用[ClientRpc]属性并将“Rpc”添加为函数名称的前缀。

This function will now be run on Clients when it is called on the Server.

当客户端调用服务器时,该函数将在客户端运行。

Any arguments will automatically be passed to the Clients as part of the ClientRpc call.

作为ClientRpc调用的一部分,任何参数都将自动传递给客户端。

For more information on the [ClientRpc] attribute, please see the page on Remote Actions.

有关[ClientRpc]属性的更多信息,请参阅远程操作的页面。

To enable respawning we will need to create a new Respawn function in the Health script and create a call to it in TakeDamage on the Server when the player’s current health reaches 0.

要启用respawning,我们需要在健康脚本中创建一个新的Respawn函数,并在玩家当前的健康达到0时在服务器上创建一个调用。

Open the Health script for editing

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

Create a new function called Respawn with the [ClientRpc] attribute and Rpc prefix.

使用[ClientRpc]属性和Rpc前缀创建一个名为Respawn的新函数。

[ClientRpc]

void RpcRespawn()

{

    if (isLocalPlayer)

    {

        // move back to zero location

        transform.position = Vector3.zero;

    }

}

Now, in TakeDamage when the player’s currentHealth reaches 0, we need to update the code to respawn the player.

现在,当玩家的currentHealth达到0的时候,我们需要更新代码来重生玩家。

Change the code in TakeDamage to reset the player’s currentHealth to maximum.

更改TakeDamage中的代码,以最大程度重置玩家的currentHealth。

currentHealth = maxHealth;

Replace the Debug.

更换调试。

Log line with a call to RpcRespawn

使用调用RpcRespawn的日志行。

// called on the Server, but invoked on the Clients

RpcRespawn();

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;

    [SyncVar(hook = "OnChangeHealth")]

    public int currentHealth = maxHealth;

    public RectTransform healthBar;

    public void TakeDamage(int amount)

    {

        if (!isServer)

            return;


        currentHealth -= amount;

        if (currentHealth <= 0)

        {

            currentHealth = maxHealth;

            // called on the Server, but invoked on the Clients

            RpcRespawn();

        }

    }

    void OnChangeHealth (int currentHealth )

    {

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

    }

    [ClientRpc]

    void RpcRespawn()

    {

        if (isLocalPlayer)

        {

            // move back to zero location

            transform.position = Vector3.zero;

        }

    }

}

In our example the Client controls the position of the local Player GameObject.

在我们的示例中,客户端控制本地播放器游戏对象的位置。

This is because the Player GameObject has local authority on the Client.

这是因为玩家的游戏对象在客户端有地方权限。

If the Server simply sets the Player GameObject’s position back to origin when the player’s currentHealth reaches 0, the Client would override the Server as the Client has authority.

如果服务器只是在玩家的currentHealth达到0时将玩家的游戏对象的位置设置回原点,那么客户端将会在客户端拥有权限时覆盖服务器。

To avoid this, the Server instructs the owning Client to move the player's GameObject to the restart position as a ClientRpc call.

为了避免这种情况,服务器指示拥有的客户端将玩家的游戏对象移动到重新启动位置,作为一个ClientRpc调用。

This position is then synchronized across all of the Clients because of the player GameObject's NetworkTransform.

然后在所有的客户端上同步这个位置,因为player GameObject的NetworkTransform。

Save the script.

保存脚本。

Return to Unity.

回到Unity。

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中的主机按钮以作为主机启动此游戏。

Move the player GameObject.

玩家GameObject移动。

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客户端按钮以连接到主机作为客户端。

Move the player GameObject, so neither player GameObjects are at the origin point.

移动玩家的游戏对象,所以玩家的游戏对象都不在原点。

When one player shoots the other and does enough damage to reduce the target’s current health to 0, the damaged player GameObject should be sent back to origin and its current health restored to maximum.

当一个玩家射杀另一个玩家,并造成足够的伤害,将目标的当前健康降至0时,被损坏的玩家游戏对象应该被送回原点,当前的健康恢复到最大值。

Close the standalone player.

关闭独立的球员。

Return to Unity.

回到Unity。

Exit Play Mode.

退出播放模式。

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

推荐阅读更多精彩内容