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

[SOLVED] RemoveClientAuthority() still seems to be broken in 5.4.0 F3. Is this a BUG?

Discussion in 'Multiplayer' started by pKallv, Sep 13, 2016.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    This is the same problem as in http://forum.unity3d.com/threads/re...ms-to-be-broken-in-5-2-1.359149/#post-2785856

    My problem is the following:

    1) Assign client authority to object: AssignClientAuthority
    2) Move object
    3) Apply RemoveClientAuthority and object snap back to original position on client side

    Is this a bug or is it me who doing something wrong?

    Here is a code example from a test i do:
    Code (CSharp):
    1. foreach (string tagName in MP_Singleton.Instance.master_Object_List) {
    2. //            print ("> " + tagName);
    3.             temp_GameObject = GameObject.FindWithTag(tagName);
    4.  
    5.             Cmd_LocalAuthority (true, temp_GameObject);
    6.  
    7.             temp_GameObject.GetComponent<Renderer>().sortingOrder = z;
    8.  
    9.             randomX = UnityEngine.Random.Range (-0.055f, 0.055f);
    10.             randomY = UnityEngine.Random.Range (-0.055f, 0.055f);
    11.             randomX = randomX + deckStartPosX;
    12.             randomY = randomY + deckStartPosY;
    13.  
    14.             Rpc_Position (temp_GameObject, randomX, randomY, z, twistAngle);
    15.  
    16.             // Add to depth
    17.             z++;
    18.  
    19.         }
    20.  
    21.         Cmd_LocalAuthority (false, temp_GameObject); //<< TEST
    Code (CSharp):
    1. [ClientRpc]
    2.     void Rpc_Position(GameObject myGO, float ranX, float ranY, int zDepth, float twist) {
    3.  
    4.         myGO.transform.position = new Vector3 (ranX, ranY, zDepth);
    5.         myGO.transform.localEulerAngles = new Vector3(0f, 0f, twist);
    6.  
    7.     }
    Code (CSharp):
    1. [Command]
    2.     void Cmd_LocalAuthority(bool getAuthority, GameObject obj) {
    3.  
    4.         objNetId = obj.GetComponent<NetworkIdentity> ();        // get the object's network ID
    5.  
    6.         if (getAuthority) {
    7.             objNetId.AssignClientAuthority (connectionToClient);    // assign authority to the player
    8.         } else {
    9.             objNetId.RemoveClientAuthority (connectionToClient);    // remove the authority from the player
    10.         }
    11.     }
    The line: "Cmd_LocalAuthority (false, temp_GameObject);" is the testing i do where i remove authority from the last object. It snaps back to original position.

    @Wobes seems to have the same problem.
     
  2. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
  3. Deleted User

    Deleted User

    Guest

    Yes, also check this: http://forum.unity3d.com/threads/un...y-cause-unity-editor-not-responce-lag.418406/

    "Hello every one, I have problem here, if Unity Editor is server, and for example client enter vehicle, then leave, in line : bike.GetComponent<NetowrkIdentity>().RemoveClientAuthority(connectionToClient); - this one will make Editor broken, but if player disconnect at this moment it will be ok.
    In this case, if Server is Standalone, there is no lags here.
    Tested on 5.3.4, 5.4.0 F2 beta, 5.3.5, 5.3.6."
     
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    @Wobes I have seen that also.
     
  5. gintautass

    gintautass

    QA Minion Unity Technologies

    Joined:
    Oct 27, 2015
    Posts:
    46
    Could on of you file a bug report for this and send me a case number?
    @Wobes I suppose you are having a different issue where it "lags"? If so, could you file a report for this as well?

    If you could attach a project which is already set up and repro steps that would speed up the process a lot, thanks!
     
  6. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    Thanks, I will do that.
     
    Deleted User and gintautass like this.
  7. Deleted User

    Deleted User

    Guest

    I no longer have project with RemoveAuthority(), and AssignAuthority(); but anyway, I think pKallv have the same, and working on it right now.
     
  8. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    I have now filed the following: Case 832286
     
    Deleted User likes this.
  9. Deleted User

    Deleted User

    Guest

    Thank you brother.
     
    pKallv likes this.
  10. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    u welcome :)
     
  11. Deleted User

    Deleted User

    Guest

  12. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    Thanks to the great people at Unity it turned out that this was not a bug, it was my mistake :)

    What i spend at least 2 - 3 month on trying to fix they fixed immediately.

    What i missed was that i had a check for authority in my pos-sync-script that caused the problem. It is in the "Cmd_ProvidePositionToServer".

    Here is the code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Object_SyncPosition : NetworkBehaviour {
    6.  
    7.     private Transform myTransform;
    8.     [SerializeField] float lerpRate = 5;
    9.     [SyncVar] private Vector3 syncPos;
    10.     private NetworkIdentity theNetID;
    11.  
    12.     private Vector3 lastPos;
    13.     private float threshold = 0.5f;
    14.  
    15.  
    16.     void Start () {
    17.         myTransform = GetComponent<Transform> ();
    18.         syncPos = GetComponent<Transform>().position;
    19.     }
    20.  
    21.  
    22.     void FixedUpdate () {
    23.         TransmitPosition ();
    24.         LerpPosition ();
    25.     }
    26.  
    27.     void LerpPosition () {
    28.         if (!hasAuthority) {
    29.             myTransform.position = Vector3.Lerp (myTransform.position, syncPos, Time.deltaTime * lerpRate);
    30.         }
    31.     }
    32.  
    33.     [Command]
    34.     void Cmd_ProvidePositionToServer (Vector3 pos) {
    35.         syncPos = pos;
    36.     }
    37.  
    38.     [ClientCallback]
    39.     void TransmitPosition () {
    40.         if (hasAuthority  && Vector3.Distance(myTransform.position, lastPos) > threshold) {
    41.             Cmd_ProvidePositionToServer (myTransform.position);
    42.             lastPos = myTransform.position;
    43.         }
    44.     }
    45. }
    A BIG thank you to Unity for the help! ;)
     
    gintautass likes this.
  13. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    So RemoveClientAuthority works for you? Unity marked the defect as postponed, so I'm trying to figure out if it works or not. My code seems to partially work, but having bizarre problems if I re-enable authority on an object that has had it removed, as well as objects no longer updating to the same positions on all clients.
     
  14. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    Yes it does. The problem i had was related to another script, which i missed, and not the function it self.
     
  15. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    Here is the code i use:

    Code (CSharp):
    1.  
    2.     [Command]
    3.     public void Cmd_SetAuthority(NetworkInstanceId objectId, NetworkIdentity player) {
    4.         //Rpc_Print ("Cmd_SetAuthority: objectId: " + objectId + " player: " + player);
    5.  
    6.  
    7.         var obj = NetworkServer.FindLocalObject(objectId);
    8.         var networkIdentity = obj.GetComponent<NetworkIdentity>();
    9.         var otherOwner = networkIdentity.clientAuthorityOwner;    
    10.  
    11.         if (otherOwner == player.connectionToClient) {
    12.             return;
    13.         } else {
    14.             // make it so that players can't steal authority from each other
    15.             if (otherOwner != null)
    16.             {
    17.                 networkIdentity.RemoveClientAuthority(otherOwner);
    18.             }
    19.             networkIdentity.AssignClientAuthority(player.connectionToClient);
    20.         }    
    21.     }
     
  16. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    @pKallv Thanks. That's similar to what I'm doing. Most of the problems were in my logic, although I still don't have this working 100% correctly. On the client that has authority, my objects always snap back to the position they had before they got authority, but the position is correct on the server and other clients. Moving the object on the server side seems to get it to send a new update to the object on the client and it's ok again, so there might be a workaround.

    If you had problems using hasAuthority, that may be due to a bug in the HLAPI. I found it while troubleshooting on 5.6. NetworkIdentity.OnStartServer sets authority to false on all objects that have LocalPlayerAuthority set to true. The comment for that code makes it clear they originally expected all objects with that flag to be controlled on the client. The result is that both client and server have hasAuthority = false for everything except player objects. Anything that checks that field on the server side (including HLAPI internal methods like ForceAuthority) will erroneously see the value set to false. Because ForceAuthority checks if the value is already set, it will stay incorrectly set until you set it to true by removing client authority. :)
     
    Last edited: Jun 7, 2017
  17. Deleted User

    Deleted User

    Guest

    Hey, brother, if you still have this Editor crash issue while Removing or Assigning authority - just change your Debug Level of NetworkManager, the reason of crash - it prints a lot of Warnings in one frame like "Warning: Instance not found when handling Command message" that's why Editor freezes.
     
    Last edited by a moderator: Jun 8, 2017
    pKallv likes this.
  18. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,177
    This still a problem?
     
  19. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    Yes, it's like the position is cached and then used because there is no fresh update from the server. If I move the object (say by ramming into it with an object I now control) it reappears in the correct position. NetworkTransform has a check called HasMoved and if that returns false, it doesn't send an update. I'm trying workarounds like adding a message that causes HasMoved to return true and calling it from OnStopAuthority. Perhaps I can just cache the position of the object on each update and set it back to that when OnStopAuthority is triggered.
     
  20. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    This doesn't work. Each frame, something sets this value back.

    ---edit---

    I haven't tracked down the exact cause--if it's my mistake or a bug in UNET--but if I turn off interpolation by setting the factor to 0 (this is a RigidBody2D,) the position remains correct after losing authority. I'm debugging the interpolation code, which runs in FixedUpdate before everything else in NetworkTransform, to understand how it's supposed to work.
     
    Last edited: Jun 9, 2017
  21. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    I haven't been able to think of a workaround, so have been forced to make a change in the HLAPI. I'm going to put this in a pull request for Unity (along with another bug I've mentioned above.)

    To fix, add this method to NetworkTransform, rebuild UnityEngine.Networking.dll and copy the output to your UnityExtensions folder.

    Code (CSharp):
    1.      
    2.         public override void OnStopAuthority()
    3.         {
    4.             m_LastClientSyncTime = 0;
    5.         }
    6.        
     
    Last edited: Jun 9, 2017