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

Unity 2D networking and syncing movement of character help

Discussion in 'Scripting' started by M0mSpaGh3tti, Feb 8, 2019.

  1. M0mSpaGh3tti

    M0mSpaGh3tti

    Joined:
    Apr 11, 2013
    Posts:
    2
    Hello everyone, I am trying to make a multiplayer 2d game and I am having troubles syncing the character controller movement. I am currently using Gamesparks for Realtime MP and my backend. The way gamesparks works is like everything else except we receive packets of the movement data in another script and change the variables in the player script to the movement properties. So currently as the controller for the game im using joysticks and im sending the joystick data to the server. Whenever I have the other enemy player move it keeps moving and never stops. Can anyone look at my code to see what im doing wrong. With Gamesparks i dont know if there is a way to tell when you are having incoming data packets so i can trigger the movement based on that.

    Code (CSharp):
    1.  void Update()
    2.      {
    3.          if (isPlayer)
    4.          {
    5.              bodyMovement();
    6.              armRotation();
    7.          }
    8.          else
    9.          {
    10.              otherTitanMovement();
    11.              otherTitanArmRotation();
    12.          }
    13.      }
    14. void otherTitanMovement()
    15.      {
    16.           //otherJoy is the Vector2 data witht he joystick data from the other player
    17.          otherMove.x = otherJoy.x * Time.deltaTime * speed;
    18.          otherMove.y = otherJoy.y * Time.deltaTime * speed;
    19.          if (otherPrevPos != otherMove) //they never equal each other so the character keeps drifting
    20.          {
    21.            
    22.              if (otherMove.x < 0)
    23.              {
    24.                  bodyRenderer.flipX = true;
    25.                  armRenderer.flipY = true;
    26.              }
    27.              else
    28.              {
    29.                  bodyRenderer.flipX = false;
    30.                  armRenderer.flipY = false;
    31.              }
    32.              bodyAnimator.SetBool("run", true);
    33.              controller.Move(otherMove);
    34.            
    35.          }
    36.          else
    37.          {
    38.              bodyAnimator.SetBool("run", false);
    39.              otherMove = Vector2.zero;
    40.          }
    41.          otherPrevPos = otherMove;
    42.          //controller.velocity.Set(0, 0, 0);
    43.      }
    This is how the other script updates the players values for movement

    Code (CSharp):
    1. public void UpdateOpponentTitans(RTPacket _packet)
    2.      {
    3.          for (int i = 0; i < playerTitanList.Length; i++)
    4.          {
    5.              if (playerTitanList[i].name == _packet.Sender.ToString())
    6.              { // check the name of the titan matches the sender
    7.                  playerTitanList[i].otherJoy = (new Vector2(_packet.Data.GetVector2(1).Value.x, _packet.Data.GetVector2(1).Value.y));
    8.                  break; // break, because we don’t need to update any other tanks.
    9.              }
    10.          }
    11.      }