Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NetworkTransform - interpolation?

Discussion in 'Multiplayer' started by PNordlund, Jun 21, 2015.

  1. PNordlund

    PNordlund

    Joined:
    Nov 20, 2013
    Posts:
    48
    I've got NetworkTransform working between server and client. Setting the Network Send Rate to 1 sec does exactly that, my objects position gets updated once per sec on the client. Oddly, you can't set this number to a smaller value on the editor, but I can force it from script to e.g. 1/30, in which case I get more updates - and more data traffic.

    Question: How do I enable position and rotation interpolation with Transform Sync Mode == Sync Transform?

    As a side note, releasing a new feature without proper documentation is not the right way to do it. Thanks.
     
  2. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    You could use the editor to put it to 1/29 of a second, thats the fastest you can get via editor.
    Have you tried the interpolate movement factor and rotation factor?
     
  3. PNordlund

    PNordlund

    Joined:
    Nov 20, 2013
    Posts:
    48
    Thanks for reply - I believe the send interval is in seconds, not in fraction of seconds, at least the documentation says "transform state updates are send at most once every sendInterval seconds." Putting it to 29 would mean an update every 29 seconds, which makes little sense.

    I did try to change the factors, no luck there. The documentation is also somewhat cryptic: "The larger this number is, the faster the object will interpolate to the target position."

    Looking in debugger, the target position for NetworkTransfor is 0,0,0.
     
  4. Carpe-Denius

    Carpe-Denius

    Joined:
    May 17, 2013
    Posts:
    842
    Well, if you try it you will see that it is "updates per second" and the tooltip says the same, so 29 is 1/29th of a second.

    Interpolation works for me. You could make a simple connection, a cube with a fixed movement on the server and 1 full second update interval.
    Start the build as a server, the editor as a client. You can use the editor for changing Interpolation on the fly and see which value is best for you. I have rotational at about 30 (I don't know what, maybe degrees per second..) and position at 0.5 (I guess a lerp between a and b with 0.5 as a mix)
     
  5. PNordlund

    PNordlund

    Joined:
    Nov 20, 2013
    Posts:
    48
    Thanks, it seems the documentation is just a bit fuzzy. I still can't get the interpolation to work however.

    Unity: Perhaps wikipediafy the documentation so we can easily clarify it when necessary. :)
     
  6. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    498
    There was briefly a comment section in the docs if I remember correctly. The community usually has all the details that the docs are lacking. It wasn't used enough though, which may contribute to why that feature is gone.

    Regarding the interpolation, you will find that it doesn't work on the 'transform' interpolation method. I'm not sure if this is a bug or by design. Docs don't tell you so I've been assuming bug, but can't confirm that. It would be pretty nice to have it work for transform type interpolation too.
     
  7. Royall

    Royall

    Joined:
    Jun 15, 2013
    Posts:
    118
    If someone can explain how to let ANY interpolation work on the network transform, ill get you a cookie :)
    Ive tried everything... But it looks like all the syncs modes are just some position sending..
     
  8. trialnterrorgames

    trialnterrorgames

    Joined:
    Feb 20, 2014
    Posts:
    58
    This is from another thread:
     
  9. PNordlund

    PNordlund

    Joined:
    Nov 20, 2013
    Posts:
    48
    Thanks guys. I'm implementing a simple interpolation code of my own.
     
  10. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Yes there's no built-in transform interpolation atm. I don't think its a bug, I think it's by design, and they just made it visible for all cases and not just rigidbody, even though it only works for rigidbody. But like I said in that other thread, if its a mistake they'll either hide that option if you select transform, or they'll just add interpolation to the transform too. I don't see why not, its fairly easy to do. :)
     
  11. gsus725

    gsus725

    Joined:
    Aug 23, 2010
    Posts:
    250
    Does NetworkTransform perform any prediction to compensate for latency? Or will the object always be like 100 ms behind where it really is locally? (Assuming 100 ms latency)
     
  12. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,908
    This kind of sucks. It will only update the rotation when I move. It seems like rolling your own is the only decent option.

    Here's a good tutorial that was mentioned:


    Here's some unoptimised sample code sending every fixed update:
    http://pastebin.com/Hg86CSqx

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Player_Sync_Pos : NetworkBehaviour {
    6.  
    7.         [Header("Options")]
    8.         public float smoothSpeed = 10f;
    9.  
    10.         [SyncVar]
    11.         private Vector3 mostRecentPos;
    12.         private Vector3 prevPos;
    13.  
    14.         void FixedUpdate()
    15.         {
    16.                 if (isLocalPlayer)
    17.                 {
    18.                         // If moved, send my data to server
    19.                         if (prevPos != transform.position)
    20.                         {
    21.                                 // Send position to server (function runs server-side)
    22.                                 CmdSendDataToServer(transform.position);
    23.                                 prevPos = transform.position;
    24.                         }
    25.                 }
    26.                 else
    27.                 {
    28.                         // Apply position to other players (mostRecentPos read from Server vis SyncVar)
    29.                         transform.position = Vector3.Lerp(transform.position, mostRecentPos, smoothSpeed * Time.deltaTime);
    30.                 }
    31.         }
    32.  
    33.         [Command]
    34.         void CmdSendDataToServer(Vector3 pos)
    35.         {
    36.                 mostRecentPos = pos;
    37.         }
    38.  
    39. }
     
    Last edited: Jul 18, 2015
  13. svendkiloo

    svendkiloo

    Joined:
    Dec 2, 2014
    Posts:
    11
    If it's _really_ the case that interpolateMovement does absolutely nothing if transformSyncMode has a certain value, and it's not a bug but either "by design" or something that has been deliberately left for future development, how come this isn't mentioned anywhere in the documentation? Seems odd...
     
  14. svendkiloo

    svendkiloo

    Joined:
    Dec 2, 2014
    Posts:
    11
    Thanks to Stardog, though. You got me in the right direction! :)

    I could simplify it a lot, as my transforms are only moved on the server, and added rotation as well. Also added an "snap distance" where it will move immediately, in case it gets out of sync, and an initialisation of the "most recent" to the current values in Awake, to avoid a snap to (0, 0, 0) in the first frame.

    I think you don't need the "prevPos", as you can just check against the value of the SyncVar.
     
  15. saluk

    saluk

    Joined:
    Aug 6, 2013
    Posts:
    4
    Unity devs... when syncing non-physics based objects... is when not having interpolation is the most visible. And that's the case where you don't support interpolation !?
     
  16. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Yes afaik interpolation doesn't apply to transforms. You need to put a rigidbody on so it works. But you can write your own interpolation.
     
  17. iddqd

    iddqd

    Joined:
    Apr 14, 2012
    Posts:
    500
    Yeah i have to say - this bug or feature whatever you might call it is pretty sad - how long us UNet out already?
    Unity Devs, do you know how many combined hours people have wasted trying to get interpolation to work?

    Hide the interpolation for transform and explain why it's not supported or better: just fix it.
    NetworkTransform is probably one of the most important scripts for networked games.

    By the way: Interpolation doesn't work for me when i have a kinematic rigidbody attached, only when it's not kinematic.
     
    jczaban and eltronix like this.
  18. eltronix

    eltronix

    Joined:
    Sep 6, 2016
    Posts:
    4
    There is a weird edge case where setting the parameters on your PlayerPrefab to conventional kinematic options and thereafter setting the Network Send Rate to zero will receive very smooth updates on the host machine but the host machine itself does not send movement updates to clients. Weird.
     
  19. ZachHelm

    ZachHelm

    Joined:
    Jul 9, 2013
    Posts:
    4
    I don't think anyone mentioned character controllers so I'll post for posterity, since this is still easy to find when searching for the answers.

    Yes, the Sync Transform mode does not do any interpolation.

    No, you do not have to use a rigidbody, though you can. The alternative is to use a character controller and Move/SimpleMove functions with Sync Character Controller. Like other parts of Unity, such as collisions, you shouldn't just manipulate the transform for movement.

    Yep, the documentation doesn't mention this and the official multiplayer tutorial even leads you to use the transform method. I wasted time too :confused:
     
    plasmanunchucks likes this.
  20. Deleted User

    Deleted User

    Guest

    Last edited by a moderator: Apr 2, 2018