- Home /
Network command is running on client?
Ok so I am trying to set up the respawn system for a multiplayer first person shooter. My understanding is that commands can only be called on client and only run on server, and similarly clientRPC's can only be called on server and only run on client. I am using the following code:
[Command]
public void Cmd_Respawn()
{
Health = 100;
Rpc_Respawn();
}
[ClientRpc]
void Rpc_Respawn()
{
RespawnCanvas.gameObject.SetActive(false);
dead = false;
r.constraints = RigidbodyConstraints.FreezeRotation;
transform.localRotation = Quaternion.identity;
//TODO respawn at a spawn point
transform.position = new Vector3(0, 2, 0);
if(isLocalPlayer)
{
//Update our Position
Cmd_UpdateServerTransform(transform.localPosition, transform.localRotation.eulerAngles, Head.localRotation.eulerAngles.x);
}
}
When the player is dead a UI is visible that lets them click a re spawn button. That button calls Cmd_Respawn() on the local client. When I do I get the error "RPC Function Rpc_Respawn called on client."
I don't understand what I am doing wrong. If Cmd_Respawn() is called on the client then is should run on the server so when Rpc_Respawn is called, it should be being called on the server, sending the instructions to re spawn the player to all clients, including the one that initiated the re spawn, but instead it is is apparently trying to run the entire command on the client?
Why is the Command being run on the client not on the server?
Some additional information: The Player class this code appears in extends from an Entity class, which extends from NetworkBehavior. Not sure if NetworkBehavior doesn't play nice with inheritance?
There is a different Command elsewhere in Player that is responsible for updating the players position via sync vars (I don't use a network transform) and that is working fine. So its not all commands, just this re-spawn command.
Edited because I realized that the entire command is being run on the client, not just the ClientRpc call. In fact, if I create a Respawn() function with the same code but no [Command] attribute, the behavior is exactly the same. For some reason the client is treating the Cmd_Respawn() function like a normal function and not a command...
Answer by CiberX15 · Oct 08, 2017 at 05:37 AM
I figured it out! (and by I, I mean not me at all but Thammuz90 on redit: https://www.reddit.com/r/Unity3D/comments/3ibx25/unet_commands_running_on_client_instead_of_server/
So apparently it did not like me calling the Cmd_Respawn() function directly. I don't know if its any external class attempting to call the command, or some weird interaction with the UI button calling it. In any case the solution was for the UI button to call Respawn() (a normal public function) on the Player, which in turn called Cmd_Respawn(). Doing it this way caused the Cmd to properly run on server and everything worked as expected.
So now my code looks like this:
public void Respawn()
{
Cmd_Respawn();
}
[Command]
public void Cmd_Respawn()
{
Health = 100;
Rpc_Respawn();
}
[ClientRpc]
void Rpc_Respawn()
{
RespawnCanvas.gameObject.SetActive(false);
dead = false;
r.constraints = RigidbodyConstraints.FreezeRotation;
transform.localRotation = Quaternion.identity;
//TODO respawn at a spawn point
transform.position = new Vector3(0, 2, 0);
if(isLocalPlayer)
{
//Update our Position
Cmd_UpdateServerTransform(transform.localPosition, transform.localRotation.eulerAngles, Head.localRotation.eulerAngles.x);
}
}
I agree with Thammuz90 that if this is intentionally blocked, there should be an error or warning letting us know that calling a Cmd from an external class will not run the function as a Cmd. Unless this is a bug and it should be running the Cmd when an external class calls it.
Your answer
Follow this Question
Related Questions
Unity networking solution (PUN or Bolt) 0 Answers
Unity multiplayer solutions: Photon, Unity Networking - what else and in what way is good? 0 Answers
Unity Multiplayer Design. Is it complicated to implement and learn? 1 Answer
Network Game - all players respond to same input 0 Answers
Multiplayer Object spawned by client does not show up on host 1 Answer