Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to setup different player prefabs

Discussion in 'Multiplayer' started by nporaMep, Jun 11, 2015.

  1. nporaMep

    nporaMep

    Joined:
    Oct 31, 2014
    Posts:
    33
    I read manual on UNET but still can't understand how to do this:
    e.g. I have "Player mode" and "Spectator mode" in game and client should be able to tell server in which mode is he connecting.
    And so I want to spawn different prefabs for different types of players.
    Also on top of this I want to spawn spectator prefab only on server and on client which is connecting as spectator, without spawning and syncing spectator prefab on all other clients.
    I also want to have different set of Command/ClientRPC for spectator and actual player.

    Another question how to implement something like chat system in new system. E.g. 2 players are on scenes ClientScene1, ClientScene2, server is on scene ServerScene and they all need to have an object with Chat component which have Command/ClientRPC methods and do chatting.

    How can I do all of this with new UNET system?
     
  2. SeriousBusinessFace

    SeriousBusinessFace

    Joined:
    May 10, 2014
    Posts:
    127
    Assuming you're using the NetworkManager, which has space for one player prefab, the solution is to make your game data-driven. A spectator player object is just a player object with the "IsSpectator" behaviour enabled and the "IsPlaying" behaviour disabled; and vice-versa for a player who's playing the game.

    The final solution can be more complex (and probably will be), but that should get you started. :)
     
  3. nporaMep

    nporaMep

    Joined:
    Oct 31, 2014
    Posts:
    33
    That is what I thought also.
    Thing is I will have like 7-8 different applications (including different scenes) all of which have their own set of Scripts/Prefabs/Sprites/Materials etc. and having an object with NetworkIdentity on each scene which has components for each of 7-8 applications will mean everything from every app will be included as dependency into every build.
    Only maybe if I do heavy eventing on everything and therefore decouple those network syncing components from all the objects on scene.
     
  4. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    488
    If you need more advanced functionality you can override the spawn methods. You can write your own logic to select Prefabs. There is some good info on how to do that in the documentation.
     
  5. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    You can derive a class from NetworkManager and override OnServerAddPlayer. That is where player objects are created.

    Remember to call:

    NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);

    for the player object that you create in your override.
     
  6. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    Hi !

    there is probably a step missing. It seems we need to know if the connected player is "Spectator" or "Player".
    I don't see any login data we can send to the server right ?
    So, I guess the step is:
    OnServerAddPlayer() => server ask player mode (ClientRpc) => client answer (Command) =>server spawn the right prefab.
     
  7. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    you can add a player at any time, it doesnt have to be inside that function. That function is called when a client asks to add a player. This is done automatically, but can be disabled in the NetworkManager settings.

    So for spectators, dont add a player at all, if they really cannot do anything.

    If they can, then make a separate network message for each type of player. MsgAddPlayer, MsgAddSpectator and handlers on the server.

    See the MasterServer example project for message processing examples.
     
  8. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    I assume the spectator can move a camera.
    You are right, a custom message will work :)
     
  9. joostbos

    joostbos

    Joined:
    Feb 4, 2015
    Posts:
    60
    I am working on a project with multiple PlayerPrefabs. I created my own NetworkManager and override OnServerAddPlayer, but it never gets called. I added a debug line as the first one and it does not show up in the log.
    I set the log level to Developer and then I see the following lines in the log:

    Server accepted client:1
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    NetworkManager:OnServerConnectInternal
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    Server event: host=1 event=DataEvent error=0
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    NetworkManager:OnServerReadyMessageInternal
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
    Ready with no player object
    UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

    I left the PlayerPrefab empty, added my PlayerPrefabs to the list of registered prefabs in the NetworkManager editor, unchecked Auto Create player and set the Player Spawn Method to Round Robin (although each scene has only one NetworkStartingPoint).

    What's going wrong? Do I need to override other methods?
     
  10. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Same here, I'm trying to spawn Player Customize Character Models.
    So after customizing and saving a character I'm Recreating the Character, as a gameobject from the saved character data, in my own NetworkManager which inherits from unity's NetworkManager like the docs say, but the OnServerAddPlayer() method doesn't seem to get called?
     
  11. sovium

    sovium

    Joined:
    Mar 8, 2016
    Posts:
    27
    .
    Hello, If you don't register your prefab as a player, I imagine the NetworkManager's OnServerAddPlayer won't be called automatically, so I assume you'd have to call something like this when you want to add your player to the scene.
     
    jister likes this.