简体   繁体   English

统一网络 | ClientRpc 在错误的玩家对象上被调用

[英]Unity Networking | ClientRpc being called on the wrong player object

The Problem: My RpcDie method is being called on the wrong GameObject.问题:我的RpcDie方法被错误的RpcDie调用。

Ie: When Player A kills Player B, it is Player A who dies - not Player B.即:当玩家 A 杀死玩家 B 时,死亡的是玩家 A - 而不是玩家 B。

But this is only locally.但这只是局部的。 So the behavior above only happens on Player A's game, and on Player B's game everything seems to be happening as expected.所以上面的行为只发生在玩家 A 的游戏中,而在玩家 B 的游戏中,一切似乎都在按预期发生。 The opposite is also true.反之亦然。

So: Player A kills player B. From Player A's perspective they are dead and Player B is alive.所以:玩家 A 杀死了玩家 B。从玩家 A 的角度来看,他们已经死了,而玩家 B 还活着。 From Player B's perspective, they have died and Player A is alive.从玩家 B 的角度来看,他们已经死了,而玩家 A 还活着。


In the Update method of my PlayerHealth script I check to see if the players hp is less than 1. If it is, then I call the CmdDie method:在我的PlayerHealth脚本的Update方法中,我检查玩家的 hp 是否小于 1。如果是,则调用CmdDie方法:

if (playerHealth < 1)
     CmdDie(); 

From CmdDie I call my RpcDie method:CmdDie我调用我的RpcDie方法:

// Calls RpcDie
[Command]
public void CmdDie ()
{
    RpcDie(); 
}

// Removes the player from the game when killed by another player
[ClientRpc]
void RpcDie ()
{
    // Checks
    if (!isLocalPlayer || PlayerUnit.isDead) return;

    // Toggles + updates     
    StopAllCoroutines(); 
    GameObject.Find("MapManager").GetComponent<GameManager>().playerCount--;
    GetComponent<Renderer>().enabled = false;
    PlayerUnit.isDead = true; 

    // Ragdoll
    ragdoll.SetActive(true); 
    foreach (Transform t in ragdoll.transform) 
       t.transform.position = gameObject.transform.position;
}

From debugging I know that the player's health is being subtracted on the correct GameObject.从调试中我知道玩家的健康在正确的游戏对象上被减去。 It is only when a player dies that this 'switching' occurs.只有当玩家死亡时才会发生这种“转换”。

From my assumptions, playerHealth is not stating "who" the player actually is;根据我的假设, playerHealth并没有说明玩家实际上是谁; whether if its Player A or Player B .无论是Player A还是Player B The script does not know that health should be reduced for Player B , rather than Player A .脚本不知道应该减少Player B而不是Player A生命值。

Each player would have a PlayerID ;每个玩家都有一个PlayerID hence, Player A would have PlayerID: 1 , and Player B would have PlayerID: 2 .因此, Player A将拥有PlayerID: 1Player B将拥有PlayerID: 2

A more technical scenario would be the Client to Server communication.更具技术性的场景是客户端到服务器的通信。 When Player A would want to attack Player B , the client (which is Player A ) would send an RPC request to the server that he want to attack Player B .Player A想要攻击Player B ,客户端(也就是Player A )会向服务器发送一个RPC请求,他要攻击Player B

The server would receive a "Packet" containing the information sent from the RPC , including: PlayerID which is initiating the attack (in this case it is PlayerID: 1 (A)), and the other PlayerID who has been attacked ( PlayerID: 2 ).服务器将收到一个“包”包含从该信息一起发送RPC ,包括: PlayerID其发起攻击(在这种情况下, PlayerID: 1 (A)),以及其他PlayerID谁被攻击( PlayerID: 2 )。 There would be more information in the packet for instance the WeaponID , and so on.数据包中会有更多信息,例如WeaponID等。

Lastly, the server would send another RPC to everyone which pseudo code would be something like ReducePlayerHealth(int playerID, int healthAmount, RPC.NotifyAll)最后,服务器会向每个人发送另一个RPC ,其伪代码类似于ReducePlayerHealth(int playerID, int healthAmount, RPC.NotifyAll)

That method would send each and every player in the game a packet in which the client would reduce the health of that specific player.该方法将向游戏中的每个玩家发送一个数据包,客户端将在该数据包中降低该特定玩家的健康状况。 The server must always be in command;服务器必须始终处于指挥状态; therefore, the server sets the data, and the client reads the data.因此,服务器设置数据,客户端读取数据。

PS: Had to post it as an answer since I could not input more characters under the comment section of your question. PS:不得不将其作为答案发布,因为我无法在您问题的评论部分下输入更多字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM