Unity 2D networking movement syncing help
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.
void Update()
{
if (isPlayer)
{
bodyMovement();
armRotation();
}
else
{
otherTitanMovement();
otherTitanArmRotation();
}
}
void otherTitanMovement()
{
//otherJoy is the Vector2 data witht he joystick data from the other player
otherMove.x = otherJoy.x * Time.deltaTime * speed;
otherMove.y = otherJoy.y * Time.deltaTime * speed;
if (otherPrevPos != otherMove) //they never equal each other so the character keeps drifting
{
if (otherMove.x < 0)
{
bodyRenderer.flipX = true;
armRenderer.flipY = true;
}
else
{
bodyRenderer.flipX = false;
armRenderer.flipY = false;
}
bodyAnimator.SetBool("run", true);
controller.Move(otherMove);
}
else
{
bodyAnimator.SetBool("run", false);
otherMove = Vector2.zero;
}
otherPrevPos = otherMove;
//controller.velocity.Set(0, 0, 0);
}
This is how the other script updates the players values for movement
public void UpdateOpponentTitans(RTPacket _packet)
{
for (int i = 0; i < playerTitanList.Length; i++)
{
if (playerTitanList[i].name == _packet.Sender.ToString())
{ // check the name of the titan matches the sender
playerTitanList[i].otherJoy = (new Vector2(_packet.Data.GetVector2(1).Value.x, _packet.Data.GetVector2(1).Value.y));
break; // break, because we don’t need to update any other tanks.
}
}
}