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

Showcase Mirror - Open Source Networking for Unity

Discussion in 'Multiplayer' started by mischa2k, Aug 11, 2016.

  1. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    190
    Will this work using a standalone authoritative server setup with latency correction(interpolation/extrapolation)?
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress: still playing around with different NetworkTransform interpolation methods. I found one that I really like, here is a gif again: https://gyazo.com/6f258fd69cc405b66059812abc0b8879

    The gray dot is the ghost, which represents the last OnDeserialize call that the client received. The client follows that ghost pretty well and smoothly. This is a 500ms send interval by the way, which was used by an MMO I played 8 years ago. In 2017, we could get away with 250ms or 100ms and get even better results.

    Thanks, will do.

    If this works with the default HLAPI then it works with HLAPI Pro too.
     
    Last edited: Aug 3, 2017
  3. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151

    I get 300 ms with Unet limited at 4k bandwith on my position synchronization.
    You can get 150 ms or 100 ms with a hight sending rate :)

    If you can get a better synchronization than playing the past in 300ms, I am curious :p.
    (Good luck to synchronize "when" 300ms is ... I rewrote all the NetworkManager to have mine which suit me 100% for problems like that :D).
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Well the least you can get is dependand on your sendrate. If you send once every 10 seconds. The least you can have is 100ms. (That's the last two). If you send 20 times a second. The least is 50ms (That is if you only interpolate and don't do any extrapolation)
     
    Last edited: Aug 3, 2017
  5. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Sorry I might have explained this wrong. We can of course use 100ms or 50ms send rates easily. What I meant was that 8 years ago that MMO I played used 500ms to minimize bandwidth. But these days everything is 10x as fast and so even 100ms won't be a big deal anymore.
     
  6. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Correct, And even 100 ms is really low for realtime games. Usualy 50 - 75 is the norm there. (15-20 sends per second). But with my post above I was speaking about Interpolation latency. But sendrate is fairly game specific
     
  7. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    15-20 send per second doesn't mean 50-70 ms.

    I synchronize at 15 send per second, on an unreliable channel (better for this things I rode...). With Unet you get 50-75 ms minimum (optic fiber or not, it's Unet Server ...) + the delay between your post and the sending. For a reason the delay is very very hight, i can't get better than say..200 ms (for pure smooth movement). So I secure to 300 ms.

    With an highter frameRate (40-60) you get your 100 ms, but you reach the 4k bandwidth limit (The Command/RPC with a Vector2 and the time when you send it)...

    And you have to synchronize "when" is 300ms between client and server and others clients. It's a mess that ServerTimeStamp is not synchronize between all clients... you have to synchronize it by yourself... Or say me if there is an other way to do it :)

    Extrapolation is great yes, but when you have a character which can jump, use tools to change movement etc.. I don't know how to do that ^^ .
     
    Last edited: Aug 4, 2017
  8. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Good morning everyone. Going to work on the NetworkTransform's rotation interpolation today. Hopefully to release a test version soon.

    UNET always waits for a few more other packets before it actually starts sending them out. You can modify that in the global config's send delay.
     
    xVergilx likes this.
  9. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress: rotation synchronization + smooth interpolation: https://gyazo.com/176e66e3cfb9c535671e155645f3f2f6
    Note that the little twitch happens when going from 360 degree to 0 degree, where the interpolation goes all the way backwards. A better solution will come later, what matter is that the networking part works pretty well.
     
    chiapet1021 and xVergilx like this.
  10. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    chiapet1021 and xVergilx like this.
  11. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress: finished Rigidbody movement and rotations. Working on rotation compression modes now:
    2017-08-04_networktransform_b_lots.png

    None: 12 byte
    Some: 6 byte
    Much: 3 byte
    Lots: 2 byte (which is kinda funny if you do the math).

    For comparison, the old NetworkTransform's compression used:
    None: 12 byte for XYZ
    Low: 6 byte for XYZ
    High: 6 byte for XYZ

    Will do some more testing tomorrow and then release the next version with the new NetworkTransform component.
     
    Last edited: Aug 4, 2017
  12. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Update: NetworkTransform finished and released. I uploaded the DLLs to the stable version in the first post.
    • [2017-08-05] NetworkTransform reimplemented from scratch
      • Problem: NetworkTransform was bloated with lots of components, all together 2144 lines of code. No interpolation. No NavMeshAgent consideration. High and Low compression modes did the same. No distinction between Kinematic and nonKinematic Rigidbodies. Too many computations.
      • Solution: reimplemented it from scratch. 300 lines of clean and simple code. Transform/Rigidbody2D/Rigidbody3D/NavMeshAgent support (including Kinematic Rigidbodies). Interpolation. 4 compression modes, able to squeeze a Quaternion into 2 bytes and still work. Teleport detection. Great Gizmo makes it easy to debug.

    I tested it in my uMMORPG project and it works really well. If anyone finds issues, please let me know. The code is worth looking at too, the Quaternion to 2 byte compression is kinda interesting.

    And here's a screenshot of the Gizmo, which turns out to be very useful for debugging:
    2017-08-05_networktransform_gizmo.png
    The bright one is the latest DeSerialized data. The dark one is the one before that. The component always interpolates between them.

    Here is a video of Teleport Detection in action:
    https://gyazo.com/d7b6b3a3f05c32e3b0970285398a0c9e
    I click behind the fence, the NavMeshAgent moves there on the server (see bright gizmo), but the Client's send interval is too huge to notice the walk around the fence. Client realizes that the destination can't be reached a short time and then teleports there.
     
    Last edited: Aug 5, 2017
    Saishy, Possum1 and mons00n like this.
  13. Possum1

    Possum1

    Joined:
    May 19, 2017
    Posts:
    3
    Keep the improvements coming. Someone at unity really needs to take notice of this project.
     
    Kaivaan and mischa2k like this.
  14. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Woa that is great!

    If you get a NetworkAnimation working I'm more than ready to jump on this!!!
     
    mischa2k likes this.
  15. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress: I set up a HLAPI Pro Patreon page instead for those who feel like contributing. Should be better than a commercial license or a paid Asset.

    Will do.

    Thank you. I am not sure if anyone should even use NetworkAnimator. I fiund it more useful to synchronize the current state and then do animations on the client. That works really well in uMMORPG, uMOBA etc.
     
    Last edited: Aug 5, 2017
    runningbird likes this.
  16. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    304
    I think a lot of people would use the NetworkAnimator if it wasn't so poorly optimized. I find the idea of syncing the animation parameters extremely useful. I had to write my own to reduce GCallocs.
     
    Saishy likes this.
  17. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    A question, do your network transform know when not to send it's position? As in, it will not use bandwidth if the object does not move, right? (or if it moved but came back to the exact same space before the next packet time)

    Also, I went to pledge to your patreon but it's saying "per post"??? Do that means every time you post anything I pay that amount? DDD:
    Why not per month like a common patreon?
     
  18. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    So for NetworkAnimator it's just the optimization, or are there really features that do not work?

    Not yet. This can be done easily with a dirty bit later, but I want to leave this first version out there for a few days to see if anyone finds any bugs before I add more to it.

    I never used patreon before, I didn't select anything about posts anywhere. It works per-month as far as I know, but I will look in the settings if I find something related.
    Edit: found it, fixed it. Sorry about that.
     
    Saishy likes this.
  19. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Thanks! Sorry for the low pledge but it's what I can do without being a burden on my current patreon too :~

    Also something that just ticked me now, with the current installation it means my git repository would not be able to be ready to use right? Since it needs file changes in the unity application itself :c
     
    mischa2k likes this.
  20. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Thanks a lot!
    What do you mean with your git repository being ready to use?

    If you want to use HLAPI Pro in your project then all you have to do is replace the DLL files in your Unity installation, see usage guide on the first page. Right now there's no way to put the Scripts into a Unity project like with Asset Store stuff, because parts of the NetworkTransport API are 'internal'. The source code repository is mainly there for you to see what happened lately.
     
  21. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Yeah that was it.

    Because I have freelancers working with me, and I would just give them access to the git and they could work.
    But I wonder if having them mess with their unity installation would put them off :c
     
  22. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Your project should still run even if they use the built in HLAPI. I really want HLAPI Pro to be a drop-in replacement that just makes it better without breaking anything. They don't have to use it if they don't want to - even though it does make debugging easier, see ReadString bug error message improvement.
     
    Saishy likes this.
  23. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    !!! Incredible :O

    Also, shared this post on the indie game developers server <3
     
    mischa2k likes this.
  24. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Does your GetRemoteDelayTimeMS work on client? Current one is broken :c

    Edit: Also another thing, in the instruction you say to move the weaver dll, but only the dll? I'm leaving the mdb in the weaver folder...
     
    Last edited: Aug 6, 2017
  25. mons00n

    mons00n

    Joined:
    Sep 18, 2013
    Posts:
    304
    It does a ton of per frame gc allocs. Also if you have an parameter controlled by a curve it constantly gives you a warning about that even if you don't select that particular parameter to be synchronized.
     
    mischa2k likes this.
  26. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    When I changed to your hlapi my main menu broke, the clients don't show the menu and when the host selects a class I receive this error:

    Found no behaviour for incoming [ClientRpc:InvokeRpcRpcClientChooseClass] on PlayerController(Clone) (UnityEngine.GameObject), the server and client should have the same NetworkBehaviour instances [netId=7].
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
     
  27. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Did you rebuild your server/client too? It has to use the DLL files as well.

    Didn't touch that part yet, will put it on my list.
     
    Last edited: Aug 6, 2017
  28. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress:
    • [2017-08-06] NetworkManagerHUD via GUILayout
      • Problem: NetworkManagerHUD tried to emulate GUILayout with manual yspace increasing which resulted in way too much code that was way too hard to look at and debug. It also had no background, so white Labels often weren't readable if the 3D world behind it was not dark.
      • Solution: replaced GUI with GUILayout. Saves 50 lines of code. Includes neat little background box.
    This update isn't too exciting, but it bothered me a lot so it had to be done.
     

    Attached Files:

  29. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    I don't have a "server", it's a client to client game. So I tested with a build and the editor like always.


    Wait, but what about the mdb file???
     
  30. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    You can ignore the mdb file.

    You can try the latest version that I just uploaded, I added a small error message in case the assetId isn't assigned - it should never happen, but just to be sure. Other than that see if it happens with the original HLAPI too.
     
  31. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Bug persists.

    Does not happen with original HLAPI

    Edit: Also, returning to original HLAPI made all network transform broke as position is not being synced anymore.
     
    Last edited: Aug 6, 2017
  32. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    So just to be sure, is this what you do:
    • Download HLAPI Pro
    • Replace Unity DLLs
    • Open Unity Editor
    • Build your project as project.exe
    • Start project.exe and connect to it from another project.exe or from the Editor
    Because it sounds like what would happen if they use different HLAPI versions.
     
  33. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Yes I'm doing that.

    And original position sync still broken :c

    Update: Resetting the networktransform component fixes, so it seems the HLAPI Pro and HLAPI are not interchangeable at will :2

    Edit2: It seems your networktransform don't work the same way as the default one, the default one syncs from the client that has authority and not from the server.
    So their uses are basically different, you should at least have a way to toggle if it's server only or from owner.

    Also noticed something strange, I'm testing it from my own computer to my own computer and you can clearly see some hipcups in the movement.

    https://gfycat.com/FatBlaringBilby
    (Edit3: Ah sorry, it seems as the webm is recorded in a low fps you can't actually see it that much...)

    The default one is really bad, but never showed this in a 0 ping environment, so I'm guessing it's something on the interpolation or the speed guessing?
     
    Last edited: Aug 7, 2017
  34. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Can you send me your project to debug? Otherwise I can also send you previous versions of HLAPI pro for you to test, so that I know when the issue started.

    About NetworkTransform: local authority isn't supported yet, will add that very soon though!

    Unity probably loses the serialization data of the old DLL if you swap in the new one. But with a reset it should work fine. The code is at least compatible.
     
  35. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
  36. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Progress: looking at NetworkTransform local player authority now. I use it in my VOXL project, so that will be a good test setup.

    You could upload it somewhere and then send me the link. Would be best if you simplify it as much as possible if you have the time to do that.

    Will bookmark those.
     
  37. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Update: next version uploaded, see link in first post.

    Removed the AssetId replacement, thanks to @Saishy for reporting the issue. The built in one was perfectly fine, I completely missed that it uses not the asset path but the asset path to GUID.

    Anyway, back to NetworkTransform!
     
  38. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Do you use local player authority? I am implementing support for that right now.
     
  39. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Update: next version released, now with NetworkTransform local authority support. Tested it in uMMORPG and VOXL, works great. Let me know if you guys have any issues and thanks everyone for reporting bugs!
     
  40. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    With the new version you mean?
     
  41. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Thanks for letting me know, will take a look.
     
  42. notseanr

    notseanr

    Joined:
    Jan 15, 2016
    Posts:
    22
    Also NetworkReader and NetworkWriter are limited to 64K element size arrays because the array size is a 16-bit integer. This is annoying if using them for serializing to large buffers, or to files. Should fix that to be a 32-bit integer...
     
  43. akuno

    akuno

    Joined:
    Dec 14, 2015
    Posts:
    85
    This is a wet dream becoming real.

    Vis2k is doing Unity's job, Unity should pay him for that.

    This guy has even proven himself as an AssetStore developer. Unity should hire him, even for a Remote position, in case he doesnt want to move.
     
  44. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Update: next version uploaded with NetworkTransform host mode interpolation fix for local authority cases. Thanks for reporting the bug @nxrighthere .

    Will put it on my list.

    Thanks for the trust.
     
    akuno likes this.
  45. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    I didn't know you could write messages directly without a class, I'm learning so much from your code!

    Code (csharp):
    1.  
    2. // send message to server
    3. NetworkWriter writer = new NetworkWriter();
    4. writer.StartMessage(MsgType.LocalPlayerTransform);
    5. writer.Write(netId);
    6. SerializeIntoWriter(writer, true);
    7. writer.FinishMessage();
    8. connectionToServer.SendWriter(writer, GetNetworkChannel());
    9.  
     
  46. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    That part is from the original source, can't take credit for that :)
     
  47. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Yeah I'm looking too at the original, but you at least was the one who showed it to me xD

    Also, found a bug in your code, I'm editing it to make a custom one for my needs and I noticed some stuttering, seems like your fixed update is called on the owner in a server if the server is also a client (listen server).

    http://i.imgur.com/33SeUgi.png

    I basically changed it to the original one part and that fixed.

    Code (csharp):
    1.  
    2.   void EmulatedFixedUpdate() {
    3.        if (!isClient || hasAuthority) {
    4.            return;
    5.        }
    6.  
    7.        // dont run if network isn't active
    8.        if (!NetworkServer.active && !NetworkClient.active) {
    9.            return;
    10.        }
    11.  
    12.        // dont run if we haven't been spawned yet
    13.        if (!isServer && !isClient) {
    14.            return;
    15.        }
    16.  
    17.        // received one yet? (initialized?)
    18.        if (goal != null) {
    19.            // rigidbody movement?
    20.            if (UseRigidbody2D() || UseRigidbody3D()) {
    21.                // teleport or interpolate
    22.                if (NeedsTeleport()) {
    23.                    SetPositionAndRotationUniversal(goal.position, false);
    24.                } else {
    25.                    SetPositionAndRotationUniversal(InterpolatePosition(), true);
    26.                }
    27.            }
    28.        }
    29.    }
    30.  
     
    mischa2k likes this.
  48. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Can you try this version:
    Code (CSharp):
    1.         // interpolation. needs to be in FixedUpdate so that we can use
    2.         // RigidBody too.
    3.         void FixedUpdate()
    4.         {
    5.             // apply interpolation on client for all other players
    6.             if (isClient && !(isLocalPlayer && hasAuthority)))
    7.             {
    8.                 // received one yet? (initialized?)
    9.                 if (goal != null)
    10.                 {
    11.                     // rigidbody movement?
    12.                     if (UseRigidbody2D() || UseRigidbody3D())
    13.                     {
    14.                         // teleport or interpolate
    15.                         if (NeedsTeleport())
    16.                         {
    17.                             SetPositionAndRotationUniversal(goal.position, goal.rotation, false);
    18.                         }
    19.                         else
    20.                         {
    21.                             SetPositionAndRotationUniversal(InterpolatePosition(), InterpolateRotation(), true);
    22.                         }
    23.                     }
    24.                 }
    25.             }
    26.         }
    Debug messages in Update will be removed soon by the way, don't worry about them.
     
  49. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Gonna test, but any specific reason to use that?
    Also what does isLocalPlayer mean? I don't know why it's false since I am actually the local player.

    Edit: Uh, the test might take some time, I'm receiving connectionToServer null in my first update call so I have to fix that first :2
     
  50. Saishy

    Saishy

    Joined:
    Jun 3, 2010
    Posts:
    79
    Nope, still gets inside on listen server.

    http://i.imgur.com/LInXEAy.png

    Also, the error is because you are using connectionToServer which is null until it is initialized, I changed to ClientScene.readyConnection as the original from unet.

    Code (csharp):
    1.  
    2.  
    3. /** <summary> REQUIRES AUTHORITY. Sends a transform message to all clients if I'm the server, or to the server if I'm a client. </summary> */
    4.    [Client]
    5.    void SendTransformMessage() {
    6.        if (!hasAuthority) {
    7.            Debug.LogError("SendTransformMessage called on " + gameObject.name + " without authority. InstanceId: " + GetInstanceID());
    8.            return;
    9.        }
    10.  
    11.        if (ClientScene.readyConnection == null) {
    12.            return;
    13.        }
    14.  
    15.        NetworkWriter writer = new NetworkWriter();
    16.  
    17.        writer.StartMessage(CustomNetworkMessages.CustomNetworkTransform);
    18.        writer.Write(netId);
    19.        SerializeIntoWriter(writer, true);
    20.        writer.FinishMessage();
    21.  
    22.        ClientScene.readyConnection.SendWriter(writer, GetNetworkChannel());
    23.    }
    24.  
    ----
    Anyway, I have to find now why it only ever updates the position when I stop moving: https://gfycat.com/ImpressionableShamefulHuemul
     
    Last edited: Aug 8, 2017