Unity3d unet - TargetRPC call originating from Client

2.3k views Asked by At

Unet's documentation is lacking, as we all know.

I would love to get this answered, and hopefully it can help others searching later.

Here is what I am trying to do:

  1. Detect hit with raycast on player client.
  2. Indicate the hit to the dedicated server using a [Command]
  3. Notify the one client that was hit using [TargetRPC]

The issue is that the TargetRPC requires a NetworkConnection value, but the only documented way is by using ConnectToClient from that object - which I don't have and I need to get.

How do I identify the Player I hit on the client side to the server in a way that can translate into a proper NetworkConnection value.

On the local player authority object:

CmdIHitSomeone(unknown);

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

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

There are 2 answers

1
Emotitron On
NetworkConnection connnection = NetworkServer.objects[netId].connectionToClient;
0
utku On
[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>());