1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice
  3. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  4. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  5. Unity 5.5 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  7. Unity 5.6 beta is now available for download.
    Dismiss Notice

Unity Multiplayer Network Transform Documentation (interpolation)

Discussion in 'Multiplayer Networking' started by LucasZombieMontec, Jun 10, 2015.

  1. LucasZombieMontec

    LucasZombieMontec

    Joined:
    Jun 10, 2015
    Posts:
    3
    I'm trying to learn the new network features (UNet) and the Network Transform needs some explanations. What are the meanings of the values? I mean, I can understand movement threshold (only sends data if the player moved more than that) but what about snapping? What is snapping? Does it make the transform jitter on a grid? It is supposed to make the movement choppy? Can I make it 0?
    Also, interpolation isn't clear. No matter what value I plug my character movement is always choppy. Is this a linear interpolation? Does it handle prediction? Would it be a better choice to code my own network transform?

    Sorry for this messy thread, I'm just too excited that I'll be able to make 3D multiplayer games soon enought. It is a child dream of mine =)
     
    liortal and erebel55 like this.
  2. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    104
    I think I read somewhere that interpolation works depending on the movement type. In particular, it doesn't work for transform movement, but does work for rigidbody movement. Perhaps that means it will work if you put a kinematic rigidbody on it. Maybe someone else can offer more information.
     
  3. LucasZombieMontec

    LucasZombieMontec

    Joined:
    Jun 10, 2015
    Posts:
    3
    This is bad =( ... I could sync the character rigidbody anyway. Why wouldn't they apply interpolation on transform movement? It is quite easy. I tried using with the character controler movement but it was just as choppy, no interpolation applied.
     
  4. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    They'll probably get to the transform interpolation in 5.1.1 or something.

    As for other things, I think snap is a threshold at which the object just snaps to the new received position. For example if it's set to 5, and since the last received position intformation the object moved more than 5 units, it'll snap to the new position. In case of lag to prevent delayed interpolations I guess, if I'm correct. You know how in some games online players teleport all over the place when its lagging? That would be snapping. While in other games they don't do that but it takes a while for the interpolated player to reach the actual position.

    Network send rate is how many times per second do you send the new transform info. Default is 9, as in every 100ms or so.

    The rest is self explanatory I think. But ask if you don't understand. I just started exploring it like yesterday, and didn't mess much with multiplayer stuff before UNET so I'm pretty green too. :p
     
  5. AlwaysBeCoding247

    AlwaysBeCoding247

    Joined:
    Jan 27, 2014
    Posts:
    41
    @LucasZombieMontec If you don't feel comfortable implementing the interpolation yourself look on YouTube for GamerToGameDeveloper. I cant get the link but he's here on the forum too. He has a video on manually doing interpolation in his UNet series that I think is a solid base. It's good to learn how these things work a little under the hood in my opinion anyway so you can tweak to your games needs if needed
     
    Tset_Tsyung and ChrisSch like this.
  6. LucasZombieMontec

    LucasZombieMontec

    Joined:
    Jun 10, 2015
    Posts:
    3
    His tutorials aren't working with the current build. Also he has given up because of bugs and lack of documentation =(
     
  7. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    85
    Is this because of a bug or will this not be supported?

    I was under the impression that interpolation and extrapolation was handled for all movement types?
     
  8. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    104
    I don't know if it's a bug or not. I'd also like to know.
     
  9. gsus725

    gsus725

    Joined:
    Aug 23, 2010
    Posts:
    236
    Does NetworkTransform use prediction to compensate for latency?
     
  10. aquilinajake

    aquilinajake

    Joined:
    Jun 8, 2014
    Posts:
    198
    when testing this in the latest build 5.1.0 it seems that the least choppy feel is with Sync Transform. also increase the Network send rate, this should work fine if you plan on using this to test with till it gets fixed. however I do not know much about networking, but playing around with it I got some nice results without jitter. Good luck
     
  11. AubryStrawberry

    AubryStrawberry

    Joined:
    Jul 21, 2014
    Posts:
    4
    Does anyone know whether it is possible to sync a skeleton with NetworkTransform?

    So have it sync child object transforms recursively
     
  12. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    221
    I'm also hoping for more information on network transform interpolation.

    I use network transform for synching NPC monsters. I use Sync Transform with the highest send rate of 29. Interpolation seems to have no effect here. At least, I haven't noticed any differences between test values of 1.0, 5.0 and 0.1. However, it would be nice if it is added because even on LAN, monster movement is a bit stuttering and not completely smooth.
     
  13. Capn_Andy

    Capn_Andy

    Joined:
    Nov 20, 2013
    Posts:
    80
    The docs are terrible ( http://docs.unity3d.com/ScriptReference/Networking.NetworkTransform-interpolateMovement.html ) on this but I *believe* the interpolation is a simple Lerp, which means values of and higher than 1 all do the same interpolation. I see visible results with values ~0.01 to ~0.1 or so, with a maxed network send rate.

    (It seems to work fine when set to transform mode and no rigidbody, though it doesn't appear to work when nested as a child object. Add the NetworkVisualizer gameobject to help figure out what's going on)
     
    moco2k likes this.
  14. Chrispins

    Chrispins

    Joined:
    Dec 9, 2014
    Posts:
    10
    Does anybody have any info about Sync Transform interpolation yet? I can't seem to see any difference between Interpolate Mover values between 1 and 0.00001.
     
  15. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    I haven't touched the multiplayer in a while, but that interpolation setting was only for rigidbody last time I played with it. And from what I've heard, you're best off making your own system for interpolation. There's user tutorials you can find on youtube. Just make sure to seach for UNET (and maybe in time period after the release) so you get the new multiplayer videos not the old ones.
     
  16. Chrispins

    Chrispins

    Joined:
    Dec 9, 2014
    Posts:
    10
    Yea I think I found a good UNet tutorial from the Youtube channel GTGD, but I'm just worried that sending all those commands every FixedUpdate() tick would be bad for the network traffic. I don't know much about networking though, so I might be worried about nothing.
     
  17. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    I think that's a good source. You should also check out other samples and examples here on the forums. And search/ask a specific question.
     
  18. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    80
    I just got matchmaker working and on the computer that created the room, everything moves around fine. On computers that join a room, it's all jittery. I messed around with changing the setting in the inspector while it was running and saw no efffect. Does anyone know if changing them while its running is ok? or do I have to actually stop the game, make the changes and thenstart it up again?

    I was so disappointed. I got the matchmaking working and two 3d characters running around in the same scene, except the client (joined) computer, the character was all jittery.

    Anyone solved this? I wish Unity would chime in on this thread.
     
  19. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    I'm pretty sure you have to build every time you change something. Try first getting it running smooth on the same computer. In other words run a few instances of the game on the same computer at the same time.
     
  20. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    80
    thanks;
    I worried that even if I do get it running smooth local, that thoes setting won't work for a client in another city or state.
     
    Last edited: Dec 15, 2015
  21. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    I'd first get it running properly on my computer, then once I see its running the way it should, I'd start bothering moving for each computer I want to test. They're not in the same room for me, so its an issue. :D
     
  22. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    80
    Chris,
    Did you ever get the jittering to stop?
     
  23. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    I only played with it a little, so I'm not someone most suited answering you, and didn't do any special coding for interpolating. I just used the network transform set to rigidbody. And its fine for some basic stuff, but for example if you bounce off a wall, that motion will be interpolated and other players will see you bounce off of air in front of that wall. :p

    I think you're better off starting a new thread describing your issue.
     
  24. SweatyChair

    SweatyChair

    Joined:
    Feb 15, 2016
    Posts:
    29
    A year passed and the document still unclear, guess we still need to spend time implementing our own interpolation system?
     
  25. TheOnlyProphet

    TheOnlyProphet

    Joined:
    Aug 29, 2016
    Posts:
    7
    Let me know if you found anything. I have a problem where the ball is like almost moving smoothly, but in the collision moment the ball kind of goes a bit inside my player(only on client that joined host).
     
  26. Wobes

    Wobes

    Joined:
    Mar 9, 2013
    Posts:
    109
    Put this script on your player Object (dont forget to delete NetworkTransform component)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetworkSyncTransform : NetworkBehaviour
    6. {
    7.     [SerializeField]
    8.     private float _posLerpRate = 15;
    9.     [SerializeField]
    10.     private float _rotLerpRate = 15;
    11.     [SerializeField]
    12.     private float _posThreshold = 0.1f;
    13.     [SerializeField]
    14.     private float _rotThreshold = 1f;
    15.  
    16.     [SyncVar]
    17.     private Vector3 _lastPosition;
    18.  
    19.     [SyncVar]
    20.     private Vector3 _lastRotation;
    21.  
    22.     void Update()
    23.     {
    24.         if (isLocalPlayer)
    25.             return;
    26.  
    27.         InterpolatePosition();
    28.         InterpolateRotation();
    29.     }
    30.  
    31.     void FixedUpdate()
    32.     {
    33.         if (!isLocalPlayer)
    34.             return;
    35.  
    36.         var posChanged = IsPositionChanged();
    37.  
    38.         if (posChanged)
    39.         {
    40.             CmdSendPosition(transform.position);
    41.             _lastPosition = transform.position;
    42.         }
    43.  
    44.         var rotChanged = IsRotationChanged();
    45.  
    46.         if (rotChanged)
    47.         {
    48.             CmdSendRotation(transform.localEulerAngles);
    49.             _lastRotation = transform.localEulerAngles;
    50.         }
    51.     }
    52.  
    53.     private void InterpolatePosition()
    54.     {
    55.         transform.position = Vector3.Lerp(transform.position, _lastPosition, Time.deltaTime * _posLerpRate);
    56.     }
    57.  
    58.     private void InterpolateRotation()
    59.     {
    60.         transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(_lastRotation), Time.deltaTime * _rotLerpRate);
    61.     }
    62.  
    63.     [Command(channel = Channels.DefaultUnreliable)]
    64.     private void CmdSendPosition(Vector3 pos)
    65.     {
    66.         _lastPosition = pos;
    67.     }
    68.  
    69.     [Command(channel = Channels.DefaultUnreliable)]
    70.     private void CmdSendRotation(Vector3 rot)
    71.     {
    72.         _lastRotation = rot;
    73.     }
    74.  
    75.     private bool IsPositionChanged()
    76.     {
    77.         return Vector3.Distance(transform.position, _lastPosition) > _posThreshold;
    78.     }
    79.  
    80.     private bool IsRotationChanged()
    81.     {
    82.         return Vector3.Distance(transform.localEulerAngles, _lastRotation) > _rotThreshold;
    83.     }
    84.  
    85.     public override int GetNetworkChannel()
    86.     {
    87.         return Channels.DefaultUnreliable;
    88.     }
    89.  
    90.     public override float GetNetworkSendInterval()
    91.     {
    92.         return 0.01f;
    93.     }
    94. }
    btw check my blog, for source code of a project.
    https://wobesdev.wordpress.com/2016/09/09/networking-on-unity5/
     
  27. TheOnlyProphet

    TheOnlyProphet

    Joined:
    Aug 29, 2016
    Posts:
    7
    I tried this code, but the ball is still wierd and jittering, but if I lower the lerprate it becomes smooth, but inaccurate. The wierd thing is that I have 2 players which have the Network Transform component synchronizing Rigidbody2D and they look smooth and accurate, but when I put the same component on my ball it isn't smooth although it is accurate (accurate meaning that it follows the same path as in host/server).
     
  28. TwistedSage

    TwistedSage

    Joined:
    Dec 12, 2012
    Posts:
    13
    This is interesting. I would like to hear from Unity on the build in interpolation (if any). I tried rigidbody and transform and the only thing that makes it appear "smoother", is when I up the transfer rate. The interpolation options appear to do nothing at all...
     
  29. Wobes

    Wobes

    Joined:
    Mar 9, 2013
    Posts:
    109
    Try to do something like this: you need to disable your Rigidbody for other players by default in player Prefab isKinematic must be false, and true isKinematic only for player, just try to call onstartlocalplayer method like this public override void OnStartLocalPlayer() { isKinematic = true; } (Just example). Otherwise, you will get somerhing like you told me.
     
  30. TheOnlyProphet

    TheOnlyProphet

    Joined:
    Aug 29, 2016
    Posts:
    7
    Well I tried it, but now my client can't hit the ball at all because it is kinematic. As I said before , my players are moving just fine with NetworkTransform component, but my ball which is spawned by Server(which is the host basically) isn't smooth and accurate at the same time.

    I thought about it and I think that all I need to do is get the position of the ball at the moment and get the new updated position and then smoothly move the ball to that position(if that makes any sens) which I think is called interpolation. Well I have to learn more about this I guess.
     
  31. Wobes

    Wobes

    Joined:
    Mar 9, 2013
    Posts:
    109
    idk why, but in my case everything goes smooth and with interpolation of rot and pos.
     
  32. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    48
    They have to rewrite all this component.

    It's NOT the general approache for network movement synchronisation.
     
  33. emotitron

    emotitron

    Joined:
    Oct 9, 2016
    Posts:
    34
    At a glance, is this code not interpolating opponent player objects between your own interpolated positions rather than absolute time-stamped state changes from the server? It seems what is needed is some kind of callback that triggers when an enemy transform state is received, and then that position should be timestamped (either locally or ideally by the opponent client). To do this right I think I would need to know the last synced Transform and its time, and the current synced transform and its time... and from that I would derive the velocity vector to apply each frame until a new correct position arrives?

    If that all makes sense.

    [edit] Looking at the code closer, it looks more like you are creating a trailing average of where the sync transforms were - rather than updating to the correct position with each sync and interpolating frames between syncs based on the implied velocity?
     
    Last edited: Dec 17, 2016
  34. Wobes

    Wobes

    Joined:
    Mar 9, 2013
    Posts:
    109
    I'm Lerping in my Update function the current Transform pos of Client with incoming Vector3 from SyncVar, so I always Interpolate my pos and rot each frame. As a result we have a smooth moves even with 150+ ping.
    If you want to Sync Rigidbody and physics, huh, a way you can do, allow to client to play with physics but send only transform, so You have next: The player prefab with isKinematic by default, in method OnStartLocalPlayer, you are allow your Avatar to control rigidbody but instead of sending velocity e.t.c, you can send position and rotation.


    [​IMG]
    Desc: the ghost is a RealTime position of client player, fully black is a Interpolated transform position.
     
    Last edited: Dec 18, 2016
  35. emotitron

    emotitron

    Joined:
    Oct 9, 2016
    Posts:
    34
    Yeah, that was what it was looking like - interpolation of past positions rather than extrapolating guesses at future positions. It generates a bit of additional latency in how the players see one another over the network, but the reward is much smoother movement (since there is no prediction going on). As I progress with my first try at a network game I am going to see if I can find a happy medium of the two - such as lerping between the last known position and my predicted position or something. Thanks for the reply!

    I have been having pretty good success so far with using rigidbody syncing combined with this transform interpolation script.
    https://gist.github.com/poemdexter/6394993

    It works, and I have yet to get a chance to figure out why it works when it seems like it shouldn't.
     
    Wobes likes this.
  36. Wobes

    Wobes

    Joined:
    Mar 9, 2013
    Posts:
    109
    Thanks, nice post!
     
  37. Orr10c

    Orr10c

    Joined:
    Sep 11, 2016
    Posts:
    20
    I used the the sync rigidbody 3d, the rotation was great but the movement was very very lagy and the position updated after a long distance travel, so I tried sync character controller and the movement dramatically improved! But than the rotation became lagy, does any know of a good compromise between the two? I'll be happy to hear :)
     
  38. dansyrotyn

    dansyrotyn

    Joined:
    Oct 16, 2012
    Posts:
    1
    Hey dude, i've modified Your script. Now it can be used also for mobs. And no lag on start
     

    Attached Files:

    Wobes likes this.
  39. Wobes

    Wobes

    Joined:
    Mar 9, 2013
    Posts:
    109
    Hi, what kind of lag on start? Like the _lastPosition is Vector3.zero? and the client start to Interpolate from Vector3.zero to transform.position?