Search Unity

Client Movement issues

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

  1. TheDreamEXE

    TheDreamEXE

    Joined:
    May 14, 2015
    Posts:
    60
    Yes, I realize I just made a topic about movement with the same code not too long ago, but I felt like this issue warranted its own thread. I have a tile based game where players use raycasting to move across a board onto various tiles, but for some reason after adding Unity's Networking solution, my client is unable to move left and right! It's been bothering me for awhile, as I just can't seem to figure out any remote clues, so I figured I'd post here for some guidance.

    Movement script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class PlayerMovement : NetworkBehaviour {
    6.  
    7.     Player player;
    8.  
    9.     void Start ()
    10.     {
    11.         player = GetComponent<Player>();
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.         if(!isLocalPlayer)
    18.             return;
    19.         if (Input.GetKeyDown (KeyCode.UpArrow))
    20.         {
    21.             player.CmdMoveUp();
    22.         }
    23.  
    24.         if (Input.GetKeyDown (KeyCode.DownArrow))
    25.         {
    26.             player.CmdMoveDown();
    27.         }
    28.  
    29.         if (Input.GetKeyDown (KeyCode.LeftArrow))
    30.         {
    31.             if(player.playerID == 1)
    32.             {
    33.                 player.CmdMoveBack();
    34.             }
    35.             else if(player.playerID == 2)
    36.             {
    37.                 player.CmdMoveFwd();
    38.             }
    39.             else
    40.             {
    41.                 Debug.Log ("move bug");
    42.             }
    43.         }
    44.  
    45.         if (Input.GetKeyDown (KeyCode.RightArrow))
    46.         {
    47.             Debug.Log("Button pressed");
    48.             if(player.playerID == 1)
    49.             {
    50.                 Debug.Log("Player ID 1");
    51.                 player.CmdMoveFwd();
    52.  
    53.             }
    54.             else if(player.playerID == 2)
    55.             {
    56.                 Debug.Log("Player ID 2");
    57.                 player.CmdMoveBack();
    58.             }
    59.             else
    60.             {
    61.                 Debug.Log ("move bug");
    62.             }
    63.         }
    64.     }
    65.  
    66. }
    I'm using the right arrow for debugging purposes at the moment (more on this later)

    Player Script snippet:


    Code (CSharp):
    1. [Command]
    2.     public void CmdMoveFwd()
    3.     {
    4.         Debug.Log("G");
    5.         if(moveEnabled)
    6.         {
    7.             Debug.Log("H");
    8.             Vector2 fwdFacing = new Vector2 (fwdPoint.position.x, fwdPoint.position.y);
    9.  
    10.             if (gameObject.transform.rotation != Quaternion.identity) {
    11.                 Debug.Log("I1");
    12.                 if (Input.GetKeyDown (KeyCode.LeftArrow)) {
    13.                     Debug.Log("J1");
    14.                     RaycastHit2D moveCheck = Physics2D.Raycast (fwdFacing, Vector2.right, 0.0f, LayerMask.NameToLayer(playerTileLayer));
    15.                     if (moveCheck.collider != null) {
    16.                         Debug.Log("K1");
    17.                         Transform objectHit = moveCheck.transform;
    18.                         moveTo = objectHit.transform.name;
    19.                         cloneTile = GameObject.Find (moveTo);
    20.                         bool canMove = cloneTile.GetComponent<Tile> ().moveable;
    21.                         int cloneTileID = cloneTile.GetComponent<Tile>().playerTile;
    22.                         if(cloneTileID == playerID)
    23.                         {
    24.                             Debug.Log("L1");
    25.                             if (canMove == true) {
    26.                                 Debug.Log("M1");
    27.                                 //spotX = spotX + 2.9f;
    28.                                 animator.SetTrigger ("pMove");
    29.                                 transform.position = moveCheck.transform.position;
    30.                                 Vector2 newPosition = transform.position;
    31.                                 if(!isServer)
    32.                                     return;
    33.                                 RpcMoveUpdate(newPosition);
    34.                             }
    35.                         }
    36.                     }
    37.                 }
    38.             }
    39.             else {
    40.                 Debug.Log("I2");
    41.                 if (Input.GetKeyDown (KeyCode.RightArrow)) {
    42.                     Debug.Log("J2");
    43.                     RaycastHit2D moveCheck = Physics2D.Raycast (fwdFacing, Vector2.right, 0.0f, LayerMask.NameToLayer(playerTileLayer));
    44.                     if (moveCheck.collider != null) {
    45.                         Debug.Log("K2");
    46.                         Transform objectHit = moveCheck.transform;
    47.                         moveTo = objectHit.transform.name;
    48.                         cloneTile = GameObject.Find (moveTo);
    49.                         bool canMove = cloneTile.GetComponent<Tile> ().moveable;
    50.                         int cloneTileID = cloneTile.GetComponent<Tile>().playerTile;
    51.                         if(cloneTileID == playerID)
    52.                         {
    53.                             Debug.Log("L2");
    54.                             if (canMove == true) {
    55.                                 Debug.Log("M2");
    56.                                 //spotX = spotX + 2.9f;
    57.                                 animator.SetTrigger ("pMove");
    58.                                 transform.position = moveCheck.transform.position;
    59.                                 Vector2 newPosition = transform.position;
    60.                                 if(!isServer)
    61.                                     return;
    62.                                 RpcMoveUpdate(newPosition);
    63.                             }
    64.                         }
    65.                     }
    66.                 }
    67.             }
    68.         }
    69.     }
    Some inspector stuff:

    Note that I have a GameObject automatically set the Player ID when they join the game. As far as debugging goes, I've made it past the "Player ID1" message on my movement script, but I get absolutely nothing when CmdMoveFwd is called. I removed it earlier, but I placed another Debug.Log underneath it to make sure it went through, and it seemed that it did. I can move up and down without issues, so I have no clue why it wouldn't work left and right. I thought it was maybe the ID or rotation, but the host can move just fine and all of the values on the client look correct. If extra info is needed, I will do my best to provide. I'm just so stuck here.
     
  2. TheDreamEXE

    TheDreamEXE

    Joined:
    May 14, 2015
    Posts:
    60
    Actually, I have another inquiry. Since movement is done via raycasting, I have an empty GameObject as a child of the player. Could it be that as a client, I don't have authority over it and therefor cannot access it?

    EDIT: I suppose that wouldn't be an issue, as I can move up and down and my host can move freely. I'm still failing to see why my client can't move left and right.
     
    Last edited: Jul 13, 2015
  3. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    you have keyboard handling code in the command function.. the same keys wont be getting pressed on the server as on the client..
     
  4. TheDreamEXE

    TheDreamEXE

    Joined:
    May 14, 2015
    Posts:
    60
    LOL do I??? Wow you're totally right. That was a complete oversight on my part. Thanks for catching that. I'll get to updating that tonight and let you know how that goes. I feel like an idiot haha