Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

[UNET HLAPI] Weird position syncing when transferring ClientAuthority

Discussion in 'UNet' started by sloschi, Apr 12, 2018.

  1. sloschi

    sloschi

    Joined:
    Jun 12, 2017
    Posts:
    3
    Hi folks!

    I am trying to implement a networked version of a blobby volley style game.



    I have implemented it in a way that the player with the ball on its side has the client authority over it. This is to guarantee that the player which is next to play the ball has no lag and can hit the ball precisely. I do transfer the authority when the ball crosses over to the other side.

    When this transfer happens, the ball behaves weirdly (i have a Network Tansform Visualizer attached to the ball):


    This is the code i use to reassign the authority:

    VolleyBall.cs:
    Code (CSharp):
    1.  
    2. private Transform tr;
    3.  
    4. private NetworkIdentity ni;
    5.  
    6. public Players.Side currentSide = Players.Side.Left;
    7.  
    8. void Awake()
    9. {
    10.     tr = GetComponent<Transform>();
    11.     ni = GetComponent<NetworkIdentity>();
    12. }
    13.  
    14. void FixedUpdate()
    15.     {
    16.         if (!ni.localPlayerAuthority || isServer) return;
    17.  
    18.         if (tr.position.x > .5f && currentSide != Players.Side.Right)
    19.         {
    20.             currentSide = Players.Side.Right;
    21.             Cmd_AssignBallAuthority(Players.Side.Right.ToString(), gameObject);
    22.         }
    23.         else if (tr.position.x <= -.5f && currentSide != Players.Side.Left)
    24.         {
    25.             currentSide = Players.Side.Left;
    26.             Cmd_AssignBallAuthority(Players.Side.Left.ToString(), gameObject);
    27.         }
    28.     }
    29.  
    30.     [Command]
    31.     void Cmd_AssignBallAuthority(string side, GameObject localBall)
    32.     {
    33.         if (side == Players.Side.Right.ToString())
    34.             GameManager.instance.AssignBallAuthority(Players.Side.Right, localBall);
    35.  
    36.         if (side == Players.Side.Left.ToString())
    37.             GameManager.instance.AssignBallAuthority(Players.Side.Left, localBall);
    38.     }
    39.  
    GameManager.cs
    Code (CSharp):
    1.  
    2. [Server]
    3.     public void AssignBallAuthority(Players.Side side, GameObject ballOfTruth)
    4.     {
    5.         if (!isServer) return;
    6.  
    7.         var niBall = ball.GetComponent<NetworkIdentity>();
    8.  
    9.         var player = side == Players.Side.Left ? playerOne : playerTwo;
    10.  
    11.         if (niBall.clientAuthorityOwner != null)
    12.             niBall.RemoveClientAuthority(niBall.clientAuthorityOwner);
    13.  
    14.         niBall.AssignClientAuthority(player.GetComponent<NetworkIdentity>().connectionToClient);
    15.  
    16.         ball.GetComponent<VolleyBall>().currentSide = side;
    17.  
    18.         var rbBall = ball.GetComponent<Rigidbody2D>();
    19.         var rbOfThruth = ballOfTruth.GetComponent<Rigidbody2D>();
    20.  
    21.         rbBall.MovePosition(rbOfThruth.position);
    22.         rbBall.MoveRotation(rbOfThruth.rotation);
    23.         rbBall.velocity = rbOfThruth.velocity;
    24.  
    25.         Rpc_AssignBallAuthority(player, ballOfTruth, (int)side);
    26.         Rpc_UpdateUI((int)side, player.GetComponent<NetworkIdentity>().netId.Value);
    27.     }
    28.  
    29. [ClientRpc]
    30.     public void Rpc_AssignBallAuthority(GameObject player, GameObject ballOfTruth, int side)
    31.     {
    32.         var localBall = GameObject.FindObjectOfType<VolleyBall>().gameObject;
    33.  
    34.         localBall.GetComponent<VolleyBall>().currentSide = (Players.Side)side;
    35.  
    36.         var rbBall = localBall.GetComponent<Rigidbody2D>();
    37.         var rbOfThruth = ballOfTruth.GetComponent<Rigidbody2D>();
    38.  
    39.         rbBall.MovePosition(rbOfThruth.position);
    40.         rbBall.MoveRotation(rbOfThruth.rotation);
    41.         rbBall.velocity = rbOfThruth.velocity;
    42.     }
    43.  
    You can find the complete unity project here: https://s3-eu-west-1.amazonaws.com/cap3gmbh/public/volley-physics.zip


    Do you know what is going on here?
     
  2. sloschi

    sloschi

    Joined:
    Jun 12, 2017
    Posts:
    3
    nobody?

    it seems that the glitches are fewer when using the network transform with "Sync Transform" instead of "Sync Ridigbody2D". But they do still happen, and the overall positioning of the ball is much more jittery.

    I have read that the NetworkTransform Component is buggy, so could it be that this is one of those bugs? I started to implement my own syncing based on MessageBase, but i still would like to know if i can get the wanted behaviour with the built-in scripts.
     
  3. sloschi

    sloschi

    Joined:
    Jun 12, 2017
    Posts:
    3
    I have implemented my own syncing component (pretty basic, with commands and rpcs) and it is working like a charm. So i guess the NetworkTransform is just buggy and should not be used.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    My experience with the NetworkTransform component is it is useful for prototyping, but isn't production ready.