Search Unity

Help with RPCs (I'm probably overthinking it)

Discussion in 'Multiplayer' started by TheDreamEXE, Jul 13, 2015.

  1. TheDreamEXE

    TheDreamEXE

    Joined:
    May 14, 2015
    Posts:
    60
    So, I have a game that is tile based, and I'm trying to get movement synced across the network. So far, I've got commands up and running and players are shown moving on the host's side, as expected. Here's a code snippet of my movement script that carries onto my player script:

    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         if(!isLocalPlayer)
    4.             return;
    5.         if (Input.GetKeyDown (KeyCode.UpArrow))
    6.         {
    7.             player.CmdMoveUp();
    8.         }
    9.  
    10.         if (Input.GetKeyDown (KeyCode.DownArrow))
    11.         {
    12.             player.CmdMoveDown();
    13.         }
    14.  
    15.         if (Input.GetKeyDown (KeyCode.LeftArrow))
    16.         {
    17.             if(player.playerID == 1)
    18.             {
    19.                 player.CmdMoveBack();
    20.             }
    21.             else if(player.playerID == 2)
    22.             {
    23.                 player.CmdMoveFwd();
    24.             }
    25.             else
    26.             {
    27.                 Debug.Log ("move bug");
    28.             }
    29.         }
    30.  
    31.         if (Input.GetKeyDown (KeyCode.RightArrow))
    32.         {
    33.             if(player.playerID == 1)
    34.             {
    35.                 player.CmdMoveFwd();
    36.             }
    37.             else if(player.playerID == 2)
    38.             {
    39.                 player.CmdMoveBack();
    40.             }
    41.             else
    42.             {
    43.                 Debug.Log ("move bug");
    44.             }
    45.         }
    Now, my question is how and where do I implement RPCs? The commands work fine and are updated on the host, as mentioned, but no movement is transferred back to the client's character. Do I make an RPC that mimics the CmdMove___() methods? I tried something of the sort, and it made my client's character move twice, but only on the server. RPC implementation confuses me for some odd reason, and any help would be greatly appreciated /:

    --Some other info--

    One of the move methods:

    Code (CSharp):
    1. [Command]
    2.     public void CmdMoveUp()
    3.     {
    4.         if (moveEnabled)
    5.         {
    6.             //MOVE UP
    7.             //All of the following code is similar for each direction and will only be commented on once
    8.             //creates a new vetor 2 to use in reference with the player's position
    9.             Vector2 upFacing = new Vector2 (upPoint.position.x, upPoint.position.y);
    10.             //creates a RaycastHit2D that starts at one of the empty GameObjects and goes in a certain direction
    11.             RaycastHit2D moveCheck = Physics2D.Raycast (upFacing, Vector2.up, 0.0f, LayerMask.NameToLayer(playerTileLayer));
    12.             //checks to make sure something was hit
    13.             if (moveCheck.collider != null)
    14.             {
    15.                 //We get the transform of the object we just hit so that we can access the name;
    16.                 Transform objectHit = moveCheck.transform;
    17.                 //A name is assigned to p1MoveTo so that we may use the GameObject.Find command
    18.                 moveTo = objectHit.transform.name;
    19.                 Debug.Log (moveTo);
    20.                 //The command is then used and passed on to our newly cloned tile. This is done so that tile properties are interactable with the player in real-time
    21.                 cloneTile = GameObject.Find (moveTo);
    22.                 bool canMove = cloneTile.GetComponent<Tile> ().moveable;
    23.                 int cloneTileID = cloneTile.GetComponent<Tile>().playerTile;
    24.                 if(cloneTileID == playerID)
    25.                 {
    26.                     if (canMove == true)
    27.                     {
    28.                         //movement in progress
    29.                         animator.SetTrigger ("pMove");
    30.                         //changes the layer
    31.                         layer = layer - 1;
    32.                         spriteRenderer.sortingOrder = layer;
    33.                         transform.position = moveCheck.transform.position;
    34.                     }
    35.                 }
    36.             }
    37.         }
    38.     }
    39.  
    P.S. if anyone can explain to me why animations don't send across the network as well, I'd love you forever. In the meantime, I'm gonna play around with this a bit more. Thanks for the input!
     
  2. TheDreamEXE

    TheDreamEXE

    Joined:
    May 14, 2015
    Posts:
    60
    UPDATE: I'm an idiot and I did overthink it.

    For reference, all I had to do was just make an RPC that took in the vector2 of my new transform and send that over. It looks something like this:


    Code (CSharp):
    1. [ClientRpc]
    2.     public void RpcMoveUpdate(Vector2 positionUpdate)
    3.     {
    4.         transform.position = positionUpdate;
    5.     }
    my movement scripts were updated to look like the following:

    Code (CSharp):
    1. [Command]
    2.     public void CmdMoveUp()
    3.     {
    4.         if (moveEnabled)
    5.         {
    6.             //MOVE UP
    7.             //All of the following code is similar for each direction and will only be commented on once
    8.             //creates a new vetor 2 to use in reference with the player's position
    9.             Vector2 upFacing = new Vector2 (upPoint.position.x, upPoint.position.y);
    10.             //creates a RaycastHit2D that starts at one of the empty GameObjects and goes in a certain direction
    11.             RaycastHit2D moveCheck = Physics2D.Raycast (upFacing, Vector2.up, 0.0f, LayerMask.NameToLayer(playerTileLayer));
    12.             //checks to make sure something was hit
    13.             if (moveCheck.collider != null)
    14.             {
    15.                 //We get the transform of the object we just hit so that we can access the name;
    16.                 Transform objectHit = moveCheck.transform;
    17.                 //A name is assigned to p1MoveTo so that we may use the GameObject.Find command
    18.                 moveTo = objectHit.transform.name;
    19.                 Debug.Log (moveTo);
    20.                 //The command is then used and passed on to our newly cloned tile. This is done so that tile properties are interactable with the player in real-time
    21.                 cloneTile = GameObject.Find (moveTo);
    22.                 bool canMove = cloneTile.GetComponent<Tile> ().moveable;
    23.                 int cloneTileID = cloneTile.GetComponent<Tile>().playerTile;
    24.                 if(cloneTileID == playerID)
    25.                 {
    26.                     if (canMove == true)
    27.                     {
    28.                         //movement in progress
    29.                         animator.SetTrigger ("pMove");
    30.                         //changes the layer
    31.                         layer = layer - 1;
    32.                         spriteRenderer.sortingOrder = layer;
    33.                         transform.position = moveCheck.transform.position;
    34.                         Vector2 newPosition = transform.position;
    35.                         if(!isServer)
    36.                             return;
    37.                         RpcMoveUpdate(newPosition);
    38.                     }
    39.                 }
    40.             }
    41.         }
    42.     }
    43.  
     
  3. darkmask58

    darkmask58

    Joined:
    May 9, 2015
    Posts:
    25
    Why not a SyncVar?
     
  4. TheDreamEXE

    TheDreamEXE

    Joined:
    May 14, 2015
    Posts:
    60
    I had no idea that you could even sync Vector2's. That would've been a great help haha
     
    darkmask58 likes this.