Unity3d_Multiplayer Netwoking16

Destroying Enemies

摧毁敌人

While the enemies can be shot by bullets and their health goes down, enemies should be destroyed when their health reaches zero.

敌人可以被子弹击中,身体降下来,当他们的健康达到零的时候,敌人就应该被消灭。

To do this, we will need to adjust the Health script to take into account the difference between players and enemies.

为了做到这一点,我们需要调整健康脚本,以考虑玩家和敌人之间的区别。

The easiest way to do this is with a check box for Destroy On Death.

最简单的方法是用一个复选框来销毁死亡。

Open the Health script for editing.

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

Add a public field for the Destroy On Death check.

在死亡支票上添加一个公共字段。

public bool destroyOnDeath;

In TakeDamage, check Destroy On Death when Current Health reaches 0 before respawning.

if (destroyOnDeath)

{

            Destroy(gameObject);

}

else

{

          // existing Respawn code     

}

The final Health 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;

    public void TakeDamage(int amount)

    {

        if (!isServer)

            return;


        currentHealth -= amount;

        if (currentHealth <= 0)

        {

            if (destroyOnDeath)

            {

                Destroy(gameObject);

            }

            else

            {

                currentHealth = maxHealth;

                // called on the Server, will be invoked on the Clients

                RpcRespawn();

            }

        }

    }

    void OnChangeHealth (int currentHealth)

    {

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

    }

    [ClientRpc]

    void RpcRespawn()

    {        if (isLocalPlayer)

        {

            // Set the player’s position to origin

            transform.position = Vector3.zero;

        }

    }

}

Save the script.

保存脚本。

Return to Unity.

回到Unity。

Select the Enemy prefab from the Project Window.

从项目窗口中选择敌人预置。

With the Enemy prefab selected,

与敌军预警队选定,

...

set the Destroy On Death checkbox to true.

将死亡复选框设置为true。


Unity3d_Multiplayer Netwoking16_第1张图片

Save the Project.

保存项目。

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

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

Shooting the enemies will cause them to lose health and when their health reaches zero, the enemies will be destroyed.

射击敌人将使他们失去健康,当他们的健康达到零,敌人将被摧毁。

The players, however, should retain the existing behaviour where they are moved back to the origin point when they “killed”, with their health restored to maximum.

然而,玩家应该保留现有的行为,当他们被“杀死”的时候回到原点,他们的健康恢复到最大。

Close the standalone player.

关闭独立的球员。

Return to Unity.

回到Unity。

Exit Play Mode.

退出播放模式。

你可能感兴趣的:(Unity3d_Multiplayer Netwoking16)