Question about RPC's and Unity

Hello,

I am very new to networking and trying to wrap my head around how to use an RPC to instantiate a player. I am trying to have my server be “Authorative” so I want to have the client send an RPC to the server and have the server instantiate the player prefab.

So - how do I do this?

I see two ways of doing this, not sure which is right:

  1. I could create an RPC on the client which has the instantiate function inside it and send that to the server. But I am not sure if the server would actually take that and call the instantiate command on the client? Confused there. Would be something like this:

    [RPC]
    void InstantiatePlayer (GameObject playerPrefab, Vector3 spawnLocation, Quaternion spawnRotation, int myGroup)
    {
    Network.Instantiate(playerPrefab, spawnLocation, spawnRotation, myGroup);
    }

OR

  1. I would send a RPC from the client with a bool to the server, and when the server receives it sees that iWantToInsantiateAPlayer is true and it instantiates the player.

Thoughts on how this is done? Thanks.

Hi!

For spawning a player on an authoritative network, you need a couple of things to happen:

  1. The player asks the server to spawn them. This is just going to be a function, depending on how you are handeling players, you want to send your identification to the server, so it knows who sent the RPC
  2. You want the server to recieve the RPC and confirm that the player can be spawned, where he will be spawned, with what, etc.
  3. The Server sends an RPC back to the client, and only to the client, the information about the spawn.
  4. The Server sends an RPC to all other player with only the information that they need to know

Since I gathered that you know how to write RPC, but not how to really use them, I will not give you an example, but instead challenge you to do it by yourself from here on :slight_smile:

Hope you get it working,
Benproductions1