standalone game micro-lag on network usage

Hi.

I am working on a simple 2 player multiplayer game, which is based on the gameplay of marble madness.
The game works as it should, except for one thing, which is really making me mad right now.

Problem:
Whenever other network applications send or receive some data over the network / internet, my game gets stuck for some milliseconds.
This also happens when I start the game twice on local machine and play it with myself!

Example:
We are playing my game (or I play it twice on local machine) and someone sends me a message on Steam.
When this happens, the game (or both games on local machine) will complety freeze for some milliseconds (micro-lag).
(with freezing I mean: not responding at all, no FPS, no inputs - but just a really short time)

Debugging:
I am not sure how to Debug this behaviour, because at the moment it happens, nothing gets executed.
The only thing I can say is: my game stops at random position in script execution.
I have tried several Debug-Messages within each script so I could see at which point it stops.
But it seems to depend on the other network applicationm, because it is never the same place where it freezes.

And as I said before, the game runs smooth when no other applications use the network.
We can play the game over internet without lag at all.
But as soon as the network is used, it will freeze for some very short time.

PC specs / network / unity:

  • CPU: Core i7 @ 3,0 GHz (8 cores)
  • GPU: GTX 580 with 3 GB VRAM
  • RAM: 16 GB DDR3 @ 1600 MHz
  • Network: Cable 100 MBit/s, average ping below 20 ms
  • Unity (free) 4.5.5f1

Networking / RPCs:

  • I have two player-controlled spheres (marbles), which get spawned with ‘Network.Instantiate()’
  • Each of them uses a ‘playerMove.cs’ Script which will check for User inputs ‘Input.GetAxis()’
  • Each of them has a ‘NetworkView’ attached (Off, not monitoring - using RPCs instead!)
  • Each of them has a ‘NetworkManager.cs’ Script, which sends / receives RPCs depending on ownership.

The RPCs are called within ‘Update()’ and send the Transform.position, Transform.rotation and Rigidbody.velocity of the Player-Spheres. (RPCMode.Others)
This works fine and without lag, as long as there is no other network application actively sending/receiving data.
One message on steam is enough to let the game freeze for a short time.

Questions:

  • How I can start to Debug/Find the problem?
  • Did anyone here experience the same problem before? (And were you able to you solve it? How?)

At the moment my RPCs are send each time, when ‘Update()’ is executed, because the Player-Spheres
are moving most of the time (even when there is no UserInput, because they are still rolling).
I just started to use Unity Networking and I am not sure if it is okay to send so many RPCs without limitations.
So the question is: Do I send to many RPCs and could this be part of the problem?

I hope I could explain the problem and the current game enviroment to make it all clear for you.
I am still learning and this could be a noob-question/problem… let me know. =)

Edit - 28. Nov: Seems to be local PC problem, not related to networking.
A new and empty project with only one moving sphere and no networking also lags,
when something gets display in my taskbar (tooltips, blinking messages / windows).

Probably it’s a problem that you send RPCs in Update(). Usually, you can’t send that many messages per second over the net. When (if) they arrive, they might cause some stuttering because each RPC has to be executed, too.
Try calling RPC every 100ms or so.

Hey tobiass, thanks for your answer.

I have added a timestamp to my Update()-Function and now I only send 30 RPCs per second.
I have also added a small buffer, which saves the received data, before it gets used.

But the problem is still there, and even with 5 RPCs per second, the game will freeze for a short moment,
when other network applications send or receive something over the network.

I have also looked into the source-code of some tutorials and other people asking ‘how to use RPCs’.
I did not find anything new. It looks like I am doing it the same way as they do it.
And the RPCs seem to do their job very well (game runs smooth and no visible lag).
But as soon as the network is used otherwise, it will freeze the game for some milliseconds. =/

Could this problem (maybe) be solved with coroutines?
I have seen some source code, where the RPC-Function itself is a coroutine, but I am not sure
if this could help here. I am not doing too much within my RPC-Function. Just receiving the data
and moving the networked player-object to its new position.

I’m not sure how often Unity sends updates like RPCs. If they get sent every 100ms, you could potentially see a jerky movement, as there are a few frames between them.
How much stuttering do you see? Some ms or seconds or…?

As I said in my first posting, I had my RPCs within the Update()-Function without any limitations.
Unity would send one RPC on each call of the Update()-Function.
(This was not the best solution, ofcourse! But it did work just fine for my two player-objects.)

However, I did change the number of RPCs to 30 Calls per Second now!
(which is suggested here: Source Multiplayer Networking - Valve Developer Community)
With 30 calls per second it looks just fine in local network and also via internet connection.
So it does not look like there is any big lag at all with this amount of calls. Which is fine!

And yes, when I set the value to 100 ms, it is indeed a bit more laggy.
With fast movement, it looks really bad at 100 ms, but 25 to 30 ms looks good enough for my game.

But there still is that problem with other network applications.
My game just stops working at all, for a very short time, whenever other applications send/receive something.
Debug.Log() Messages also show, that it does not freeze my game at the same point, but instead it just stops
whenever other applications send or receive, and therefor use the network.

And I can’t really tell how long my game freezes, but it just looks like milliseconds.
I also checked other parts of my sourcecode for errors, but could not find anything,
which would explain the freezing. Really clueless on this problem, atm.

Do you think (or know) if coroutines could solve this?
I am not quite sure (yet), how they would affect my RPCs and the general game-behaviour.