Search Unity

How can I spawn an object on one client?

Discussion in 'Netcode for GameObjects' started by Deleted User, Apr 6, 2022.

  1. Deleted User

    Deleted User

    Guest

    I am currently working on a game that I would like to be infinitely procedurally generated. On singleplayer, this isn't really a big deal as all of the computation happens on the host computer and nothing has to be sent to any clients. After working with Netcode for GameObjects for a while, it became painfully obvious that I could only spawn a certain number of items before the UNet transport throws a hissy fit. Sending all of the objects to the clients is also incredibly wasteful.

    Now, what I want is to create a bubble around each player that objects will only spawn on that client if they come within the bubble. This would allow the server to do the heavy lifting while leaving the clients to run faster and load in quicker. How would I do something like this?
     
  2. Draener

    Draener

    Joined:
    Dec 17, 2021
    Posts:
    7
    Well, if it it is procedurally generated, then you shouldn't have to run it over the network.... just run the procedure on both places. If you need to communicate changes.... then just send changes (in a view distance around your character.

    Example

    ClientRpcParams clientRpcParams = new ()
    {
    Send = new ClientRpcSendParams
    {
    TargetClientIds = new ulong[] { clientId }
    }
    };

    ReceiveWorldClientRpc(state.GetSeed(), state.GetVoxelState(), ClientRpcParams);

    The ClientRpcParams will allow you to filter who to send to, and you can get the id on network start.



    NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
    private void OnClientConnected(ulong clientId) { PlayerStats.clientId = clientId; }
     
  3. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    664
    Switching over to using the Unity transport will remove the limit on the number of network objects you can spawn.

    If you want to take control over what objects are spawned on particular clients take a look at networkObject.CheckObjectVisibility and networkObject.NetworkHide() and networkObject.NetworkShow().
     
    Deleted User and luke-unity like this.