Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unet Command only effecting server

Discussion in 'UNet' started by IroncladEarl, Feb 10, 2018.

  1. IroncladEarl

    IroncladEarl

    Joined:
    Jul 3, 2013
    Posts:
    87
    I'm having trouble with some Unet basics. I'm trying to send a command from a client, but the only thing that's being effected is the one on the server. Here is some more info:

    Code (CSharp):
    1. // Tank.cs
    2. // Extends NetworkBehavior & has NetworkIdentity & isLocalPlayer
    3.  
    4. void Update() {
    5.         if (hasAuthority) {
    6.             if(Input.GetMouseButtonDown(0)) {
    7.                 CmdMove();
    8.             }
    9.         }
    10. }
    11.  
    12. [Command]
    13. void CmdMove() {
    14.         transform.Translate(0, 0.5f, 0);
    15. }
    This is the client Tank identity:


    So I have control of the tank but it's only moving on the server and not the client that I'm clicking in.

    Any help is appreciated. Thanks
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You have a NetworkTransform on the tank object?
     
    ItsaMeTuni likes this.
  3. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    If i understand what you're trying to do:
    Code (CSharp):
    1. // Tank.cs
    2. // Extends NetworkBehavior & has NetworkIdentity & isLocalPlayer
    3. void Update() {
    4.         if (hasAuthority) {
    5.             if(Input.GetMouseButtonDown(0)) {
    6.                 if(isServer){
    7.                      RpcMove();
    8.                    }
    9.                  else{
    10.                 CmdMove();
    11. }
    12.             }
    13.         }
    14. }
    15. [Command]
    16. void CmdMove() {
    17.         transform.Translate(0, 0.5f, 0);
    18. }
    19. [ClientRPC]
    20. void RpcMove() {
    21.         CmdMove();
    22. }
    Anytime you want something to move on both sides you must do something just like this. ClientRPC and Command
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't really understand what you're trying to do. So if this is a server it calls RpcMove, but that just calls CmdMove, with still no movement on the clients. You'd want RpcMove to actually make some movement on the client and not call CmdMove again (why call RpcMove to just run a command when the server could just run the command locally without the unnecessary network back and forth?).

    Really what I'd do is throw a NetworkTransform on the object, set it up properly to sync movements to the clients from the server, and then send Commands from the clients to the server to do movements as in the original code.

    You could sync all movements with Commands and Rpcs manually, but then you may have to account for new client connections that won't have received any of the earlier Rpc position updates, but the NetworkTransform will take care of that.
     
    ItsaMeTuni likes this.
  5. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    True, sorry for making your situation worse :p