2016-12-31 110 views
2

我们都知道Unet的文档是缺乏的。Unity3d unet - 源自客户端的TargetRPC呼叫

我很想得到这个答案,并希望它可以帮助其他人以后搜索。

这里是我想要做的事:

  1. 检测与播放器客户端上的光线投射命中。
  2. 指示使用[命令]命中到专用服务器
  3. 通知的一个客户端,使用[TargetRPC]打

的问题是,TargetRPC需要NetworkConnection价值,但只有记录方法是通过使用ConnectToClient从该对象 - 我没有,我需要得到。

如何识别我在客户端打到服务器的播放器,并将其转换为适当的NetworkConnection值。

在本地选手机构对象:

CmdIHitSomeone(unknown); 

[Command] 
void CmdIHitSomeone(NetworkConnect unknown) { 
    TargetLetYourVictimFeelIt(unknown); 
} 

[TargetRPC] 
void TargetLetYourVictimFeelIt(NetworkConnection unknown){ 
    //do something bad here 
} 

回答

1
NetworkConnection connnection = NetworkServer.objects[netId].connectionToClient; 
+1

能不能请你扩大你的答案吗? –

0
[Command] 
void CmdIHitSomeone(int amountToHit, NetworkIdentity target) 
{ 
    TargetLetYourVictimFeelIt(target.connectionToClient , amountToHit);  
} 
[TargetRPC] 
void TargetLetYourVictimFeelIt(NetworkConnection target, int amount) 
{ 
    //do something bad here 
} 
// call your command by getting NetworkIdentitiy component of your target/enemy 
GameObject target; 
CmdIHitSomeone(10, target.GetComponent<NetworkIdentity>());