Search Unity

Instantiating the terrain on the server on the client when the client connects

Discussion in 'Multiplayer' started by WormChickenWizard, Mar 18, 2017.

  1. WormChickenWizard

    WormChickenWizard

    Joined:
    Jun 25, 2014
    Posts:
    18
    Hi, I have a scene that gets an array of terrain prefabs located in the resources folder, converts the names of the terrains to a list, and stores them in a drop-down as well as an okay button that takes the value in the drop-down and jumps to another scene and spawns in the terrain. How can I get that value of that terrain prefab that is spawned in on the server to the clients that connect and Instantiate it in the scene at the same location and rotation it is on the server? I don't need the terrain to be a networked object, but the client does need to know which terrain to spawn in.

    As you may or may not be able to tell already, I am not that good when it comes to networking :)
     
  2. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    244
    well making the terrain be a networked object would do it for you automatically. of course you'd need to add it to the list of spawnable objects in the NetworkManager which is a little tedious to maintain

    another approach: when the server detects a player has joined, send that player a message with your terrain data.

    so in your player script, you'll want to override the function OnStartServer, and check if (!isLocalPlayer). if it's not the local player, you'll want to call something like connectionToClient.Send(msgType, msg) to send a message to that newly connected player with your terrain info.

    if you're not sure how to send a network message, have a look at this doc - https://docs.unity3d.com/Manual/UNetMessages.html

    basically you create your own message class that extends MessageBase, and you give it the properties you need - terrainId, position, rotation. the client starts listening for the message in OnStartClient, and the server sends the message to the client as i suggested above.
     
  3. WormChickenWizard

    WormChickenWizard

    Joined:
    Jun 25, 2014
    Posts:
    18
    @robochase thanks for the reply, but what I ended up doing was syncing the name of the terrain and on client connect, I used resources.load using the name to get the terrain that was running on the server and instantiated it locally.