Game Development

In Unity/UNet: How do you correctly spawn a `NetworkPlayer`?

In Unity/UNet:
How do you correctly spawn a NetworkPlayer? Proper now, I am doing it like this from inside a NetworkManager derived class:

   public override void OnServerAddPlayer(NetworkConnection conn, brief playerControllerId) {
        NetworkPlayer newPlayer = Instantiate<NetworkPlayer>(m_NetworkPlayerPrefab);
        DontDestroyOnLoad(newPlayer);
        NetworkServer.AddPlayerForConnection(conn, newPlayer.gameObject, playerControllerId);
   }

This code snippet works fairly properly and each shoppers can talk with one another. Nevertheless, there are just a few little points that come up solely on the host:

  1. In Unity’s hierarchy-view on the host, there are solely two NetworkPlayer cases. Should not there be 4 NetworkPlayer cases on the host? Two shopper cases and two server cases? If that’s the case, do you’ve any concepts what might trigger the lacking NetworkPlayer cases?
  2. The 2 NetworkPlayer cases have each, their isClient and isServer flags set to true. However solely one of many has it is isLocalPlayer flag set. Now I’m wondering if this habits is as meant? And if that’s the case, how do you distinguish between the shopper and the server occasion of a NetworkPlayer?
  3. Two participant habits: If the distant shopper sends a [Command] that modifications a [SyncVar] on the server, then on the host, the [SyncVar]-hook is named solely on the NetworkPlayer occasion that represents the distant NetworkPlayer. The [SyncVar]-hook is just not referred to as on the host’s "isLocalPlayer-NetworkPlayer" occasion. Should not the [SyncVar]-hook be referred to as on each NetworkPlayer cases?

Any advise is welcome. Thanks!

About the author

Theme control panel

Leave a Comment