Networking in Unity is pretty simple. As someone who tried to write their own Unity / Gamemaker in XNA, I can say that they’ve really made it easy for you to do things. Not only that, but there are numerous posts on the subject, talking about how to make everything work together. Most of the answers to posts I’ve ready are pretty similar to this:
private void SetFired(bool fired)
{
if (isServer)
{
RpcSetFired(fired)
}
else
{
CmdSetFired(fired)
}
}
[Command]
void CmdSetFired(bool fired)
{
RpcSetFired(fired)
}
[ClientRpc]
void RpcSetFired(bool fired)
{
Fired = fired;
}
This is actual former code I was using. This worked for me for the longest time UNTIL I decided to write my own Network Manager, that inherited from Unity’s. It wasn’t until I decided to create my own lobby system using Unity’s matchmaking services that this code started producing lag on the client side. Literally, anything using this paradigm had some feature of lag - This code in particular was to let the server know that the player fired (not sure why i wanted to do that to start with… but that’s neither here nor there), but I had a function just like it to produce a projectile, which was having the same problem. Same thing with flipping a sprite over the network - the client always lagged. I know it wasn’t general lag, because movement itself was unaffected - which uses Unity’s Network Transformer. The sprite flipping I’m doing isn’t covered in the Animator (as far as I am aware? Now that I think about it though…I’ll research that after writing this), so the only way for me to do it was using the ClientRpc-Command Paradigm.
After sleeping on it for a bit, drinking some coffee, and partaking in other various activties to spark my brain activity, I had this crazy idea. What if… ClientRpcs were not needed at all? The point of the Command is for the Client to tell the Server “hey, i’m doing this” and for the server to say “okay, I’ll let everyone else know YOU are doing that.” The ClientRpc is for the Server to say “Hey all clients, you’re doing this.” and they do. So why would you have to write a Command to tell the Server you’re going to do something, THEN tell it to tell the Clients?
I haven’t seen a post that says this, and would love to hear some opinions on this, but if we’re to take the example above, here’s how I re-wrote the code, and everything functioned perfectly on both sides:
private void SetFired(bool fired)
{
Fired = fired;
CmdSetFired(fired);
}
[Command]
void CmdSetFired(bool fired)
{
Fired = fired;
}
No ClientRPC. I tried only with the Command, but without local set, the other clients never get the message. I applied this fix to EVERY piece of functionality that had lag attached to it,- each of which were running the original ClientRPC-Command paradigm, and it they all started functioning better. Another thing I noticed was that I was having some weird disconnect problems before applying this fix - i haven’t seen a single disconnect since.
Let me know your opinions. Is there something I’m missing here? Am I ahead of the game or have i just been reading the wrong posts? googling the wrong things?