Explanation of Networking basics

Hi,
Are there any tutorials/articles that explain the absolute basics of Unet networking? I’m very new to this (i.e. no experience with Unity legacy networking, Photon, server authoritative architecture, etc.). Coming from C# web development! :slight_smile:

I watched these excellent tutorials (http://www.gamertogamedeveloper.com/unity-5-tutorials) but I’m just emulating the steps and still don’t feel I understand what’s actually going on :frowning:

I’ve a few basic questions in case anyone has time to help:

  1. I understand there’s a host (which is a server and a client) - but is this actually two copies of the game, running side by side in memory, each with their own copy of all the code, variable values, etc? I see they “talk” using local calls, rather than network calls, etc. Seems as if there are two instances?

  2. What’s the difference between isLocalPlayer and isClient? e.g. If I run a host instance and then connect a second instance, will isLocalPlayer be true for any NetworkBehaviour running in this second instance? Or is it only for NetworkBehaviour objects spawned by the server, in the second instance? My head is melting.

  3. What’s a good way to “think” about how all the server, local client and remote client instances communicate. It seems that each has and identical copy of the code but that the path through that code is modified by judicious use of “isClient”, “isLocalPlayer”, etc. Just not sure how to approach it.

  4. Any advice on what objects should be tracked locally and which should be tracked via server? i.e. if I fire a rigidbody missile on a client, does that need to have a NetworkTransform that updates the position continuously? If it’s a rigidbody, that trajectory must be predictable on each client and could be spawned there? The answer is probably obvious to everyone but me :(!

Thanks.

Networking can be a challenge to get your head around, and UNet is no exception to this :slight_smile: I’ll see if I can tackle some of your direct questions.

  1. So when running as a host, there’s only one version of the game running. Within that game, somewhere under the hood of the UNet API sit a client and server object. It’s the state of these objects that visible things like the Network Manager and any networked objects (networkBehaviour derived components) rely on. For example when running as a host, a networkBehaviour component on an object on the server can check isServer, and only there will it be true.

As another example, on the player object spawned for the host client (on the machine running as a host), within one of that objects scripts isServer would be true. On the version of his player object on a remote client, isServer would be false.

  1. isClient is true on any client connected to a server. I’m 99% sure this will only be false on a dedicated server, as I think a host returns true for both isClient and isServer (it has both a client connected to it’s local server object, and it has a server object running as a server). Hopefully someone else can verify that.

isLocalClient is only true on a clients local version of an object over which they have authority, for example the version of their player object on their machine. So in your example your host runs a server, and on their machine on their player object isLocalClient will be true. Another client connects to the server, On the hosts version of the game on that new player object, isLocalClient will be false, as it belongs to the new player. On the new players machine, again on that new player object, isLocalClient will be true. Also on the new client, an object will have spawned for the server-player, and here isLocalClient will be false, as it belongs to the server. In simple terms, it’s a way of checking “is this my thing?” so that you can for example enable local player control.

Does that make sense? It can be quite wordy to try and explain some of these things xD

1 Like

Thank you for such a detailed reply! I’m running some tests now, based on this, to try and nail down what flags are set, when and where!

It seems another user, FStar, posted just before me on a similar issue - that this mixture of client & server code, within the same class/method is fine for simple projects but can get quite messy at larger scales. I think I’ve been a bit too ambitious for my first project but will stick with it!

Thanks!

1 Like