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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

UNet Lag

Discussion in 'UNet' started by Ben_Iyan, Dec 12, 2017.

  1. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    I've tried a simple network example where both client and server move a cube, which should be about as simple as it gets. What I have a hard time understanding, and this seems to be Unity-wide, is the initial movement lag. I've tried UNet, PUN and now Forge, and they all exhibit the same initial lag. For two instances running on the same PC (localhost), the initial message to move should, I think, be nearly instantaneous, but there is consistently a visible lag where the client (or host, if it's the client that's moving) cube lags by about half the width of the cube, and you can see it coasting to a stop after the host has stopped moving. I set up a similar test in UE4 and there is absolutely no visible lag, so I can't figure it out what it is about Unity. Do anyone have any idea why this is happening?
     
  2. Que

    Que

    Joined:
    Mar 14, 2014
    Posts:
    135
    If everything you tested got the same results. Did you not find it a bit odd?
    Have you check your internet connection?

    Many are using the network games, and
     
  3. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    Yes, I found it very odd, which is why I tried with a different engine (UE4) but still on localhost, and if there was lag, it was imperceptible, whereas every solution that I tried with Unity has the same results. On localhost, outgoing packets don't even have to go all the way down the OSI stack, so communications should be extremely fast, but with Unity, that initial message seems to be delayed. If you watch YouTube tutorials that also use a simple cube, you can see the initial lag there as well. I really (really) want to use Unity, but I can't find a way past this.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    I haven't noticed any abnormal lag with UNet or Photon and have used both in production.
     
  5. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    Hmmm, that's how it should work, but the fact that I can see the same lag in online videos makes me believe that it is not my system. Any troubleshooting ideas?
     
  6. Teila

    Teila

    Joined:
    Jan 13, 2013
    Posts:
    6,929
    Something in your Unity code? If it works with UE4, not your local internet. If it does not work in Unity with any network solution, more than likely something in your project. We use Unet without any problem and our scenes are more complicated than cubes. I will suggest to you what I do with my programmer...make a new Unity project, put in some simple stuff like capsules that move around, and then test it with Unet. If it works, then it is your original project. If not, then maybe a Unity bug...and you should report it.
     
  7. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    Every project that I tried with Unity (UNet, PUN, Forge) all used new Unity projects, as were all of the online tutorials that I tried.

    This is a link to a Forge Networking Remastered tutorial:


    If you check out around 14:14 you'll see what I mean. It's fast, but when he moves the cube in the top left, you can see the slight hesitation in starting in the bottom two windows. UE4 does not have this delay and I can't explain it.
     
  8. Teila

    Teila

    Joined:
    Jan 13, 2013
    Posts:
    6,929
    Videos can sometimes slow things down, could it just be video lag? I know whenever I record something that moves, it always appears to lag. Try making a build and see what that does.
     
  9. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    I followed the tutorial and copied what he did, and I got the exact same result :(
     
  10. Teila

    Teila

    Joined:
    Jan 13, 2013
    Posts:
    6,929
    Why would you copy what he did if he had bad results? Try the profiler and see where you are getting spikes.
     
  11. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    LOL, yeah, I guess the way I worded that sounds strange. I was following a series of tutorials to see what Forge Networking was like, and I like to follow along on my own because I find it helps me learn better. I've attached a Dropbox link to the project and I was wondering if you would mind trying it yourself. Run two instances (one can be in the editor, if you prefer) and make them windowed, 800x600 resolution, and if you have enough screen space, put one window above the other. Only the server can move its cube, so that will have to be the active window. When you move the server cube (A or D, or arrow keys) you should see a very distinct delay before the cube moves on the client screen, and it will continue moving on the client after it has stopped moving on the server.

    https://www.dropbox.com/s/1xf2n9plbpddlro/Forge Networking Remastered.7z?dl=0
     
  12. Teila

    Teila

    Joined:
    Jan 13, 2013
    Posts:
    6,929
    We do not use Forge so probably will not work on our server. But you might post this in the Forge thread and someone might be happy to help you. :)
     
  13. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
  14. Teila

    Teila

    Joined:
    Jan 13, 2013
    Posts:
    6,929
    I see. Then you may have to use UE4. Good luck!
     
  15. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    @Ben_Iyan This is because they use linear interpolation. Lerp is not suited for this. To get the perfect movement interpolation, you must use MoveTowards instead, and you also need to send the current speed of your object in addition to the position data. You can get the speed from the velocity using magnitude.
    Code (csharp):
    1. transform.position = Vector3.MoveTowards(transform.position, nextPosition, lastSpeed * Time.deltaTime);
     
    Last edited: Dec 14, 2017
    Teila likes this.
  16. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    @nxrighthere Yes, I agree, and that is how I do it. I have no problem with smoothly interpolated movement; the problem is the delay between movement starting on, lets say the host, and the movement of the networked version (e.g. a client). It's almost as if Unity delays sending that first packet.
     
  17. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    Does this happen only once after the object is spawned or does it occur every time the object starts moving?

    Do you send data with the initial position at startup?
     
    Last edited: Dec 14, 2017
  18. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    Every time the object starts moving, and I get it whether I'm using UNet, PUN or Forge, but nobody else seems to have this problem so I'm wondering if there's a setting somewhere that I'm missing.
     
  19. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    What version of Unity are you using? You tried to use different versions with a new clean project?
     
  20. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    I've tried with 5.6, 2017.1 and now with 2017.2. Yes, I always started with a clean project. Each of the projects is from following a YouTube tutorial but I don't think that would make a difference. I really appreciate the suggestions for things to try.
     
  21. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    Can you send me an example project that uses UNET?
     
  22. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    I would very much appreciate it if you could take a look. The following Dropbox link is to a project that I created by following a YouTube tutorial (i.e. there is nothing new that I've added). I originally tried it with 5.6, but the linked version is 2017.2p4 (started from scratch). Only the server has replicated movement, so what I recommend is that, to start, you run the build file and create two 800x600 windows and put them one above the other. When you move on the server, you can see the initial lag. The tutorial that I followed using Forge a couple of days ago has the same lag. Thank you for taking a look.

    https://www.dropbox.com/sh/wyu7wpxdix60klz/AABMiVsNHX5Z5NmSwPJpkFxJa?dl=0
     
  23. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    I tried your build and everything works fine on my side. I don't see this lag. The problem can be in the OS or third-party software.
     
    Ben_Iyan likes this.
  24. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    When you move the cube on the server, you don't see a slight delay on the client, and the client continuing to move briefly? Are you positive? If you are, then that's great news, sort of. Now I have to figure out what's wrong on my end. Thank you for taking the time to help me troubleshoot this. I find that when I post a question on Unity's forums, I almost always get an answer. On the UE4 forums ... meh.
     
  25. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    Yep. It starts to move smoothly without any slight delay.
     
  26. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    Well, I'm really glad to hear that it's not Unity, it's my system. Any idea what I could try to troubleshoot this further?
     
  27. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    Hard to tell. The first thing that comes to mind is to check the Firewall/Anti-malware software. This problem can also be caused by third-party software that uses something like WinDivert to manipulate network packets.
     
  28. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    Given that I don't get the problem with UE4, I'm wondering if it is something Unity specific, maybe something in Player Settings?
     
  29. nxrighthere

    nxrighthere

    Joined:
    Mar 2, 2014
    Posts:
    567
    If you're mean PlayerPrefs, then I doubt it. I'm pretty sure that the problem is not in Unity since the standalone build works fine on my PC and laptop.
     
    Last edited: Dec 17, 2017
  30. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    OK, I see what you mean. Crap. Well, if you think of anything else that I can try, please let me know. Thank you again for helping me to determine that the problem is not with Unity. I really appreciate you taking the time.
     
  31. srylain

    srylain

    Joined:
    Sep 5, 2013
    Posts:
    159
    Even if you have one instance sending data to another instance on the same PC, you can have anywhere near as much as one frame of lag (imagine if the one instance sends data on the same tick that the other instance just rendered its current frame). This depends on when each instance is updating, and could also be worse depending on the send rate. If you're only sending data 30 times a second, that's two more frames of lag.

    When making networked games you'll always have lag, that's just the nature of sending data from one thing to another.
     
  32. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    @crynryan I understand what you're saying, but unless I'm missing something else (highly likely), a 3-frame delay only equates to 50 milliseconds. While there are many studies, it seems to be the consensus that the human eye cannot detect changes in movement that last less than 100 milliseconds. What works against my argument is the fact that the two people who commented earlier detect no visible lag at all. I've sent a copy of my build to six of my friends all they all see the same thing that I do, but @nxrighthere tried that same build on his system and he saw no lag. Therefore, I'm really confused.
     
  33. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    @JWLewis777 I appreciate you "poking your head in" because I'm at a full stop (and yes, I definitely hear you regarding OCD). The machine that I'm testing on is reasonably good (i7, 16 GB RAM, nVidia GTX 660). I have tried on three other machines, plus those of my friends, and the results are the same. It's also been tested on Windows 7, 8.1 and 10 but the outcome is always the same. Given that no one else has this problem is both good and bad news. It's good in that the problem doesn't reside in Unity, but the bad is that I'm completely stumped. I may have to take the advice of @Teila and move to UE4 (yuck). Thanks again for the suggestions.
     
  34. FuzzyShan

    FuzzyShan

    Joined:
    Jul 30, 2012
    Posts:
    182
    https://forum.unity.com/threads/problem-with-onserialize-ondeserialize-rpc.510195/ Please check out the question I asked here, you may be experience same thing I am getting using hhapi, you may want to debug all your time using system.date.now.milisecond having both client and server on same computer to test out when you actually got the message in serialize and deserialize

    You should not use human eye to pin this question but to actually log all the timer to check what exactly is going on.
     
    Ben_Iyan likes this.
  35. Ben_Iyan

    Ben_Iyan

    Joined:
    May 13, 2016
    Posts:
    203
    @FuzzyShan Yes, it certainly looks like I have the same problem. JWLewis777 had the same suggestion to log the times, but I can't figure out how. The instance that I run in the editor has access to the console but the other instance is stand-alone. How did you log it? Did you write to a log file? Thanks to both of you for the input.
     
  36. FuzzyShan

    FuzzyShan

    Joined:
    Jul 30, 2012
    Posts:
    182
    I use System.DateTime.Now.Second + ":" + System.DateTime.Now.Milisecond.ToString("000"), for pc you can open two unity editor instance you just need two projects. For StandAlone, I used a UGUI to output my console debugs, you pretty much need a console GUI in runtime anyways if you are aiming to ship games(There are bunch on asset store), I am pretty sure you can do the same thing with UE4, and compare both response time.
     
    Last edited: Dec 28, 2017