- Unity3d_Multiplayer Netwoking14
- Unity3d_Multiplayer Netwoking17
- Unity3d_Multiplayer Netwoking18
- Unity3d_Multiplayer Netwoking5
- Unity3d_Multiplayer Netwoking6
- Unity3d_Multiplayer Netwoking7
- Unity3d_Multiplayer Netwoking2
- Unity3d_Multiplayer Netwoking4
- Unity3d_Multiplayer Netwoking3
- Unity3d_Multiplayer Netwoking10
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.
退出播放模式。
网友评论