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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to correctly spawn characters during multiplayer

Discussion in 'Multiplayer' started by Palimon, Jul 16, 2015.

  1. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Hey all, I'm trying to get spawning working right during multiplayer. I need to get away from the default spawning from NetworkManager, so I created a class:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetworkManagerExtended : NetworkManager
    6. {
    7.     public GameObject bulletPrefab;
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.     }
    20.  
    21.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    22.     {
    23.         var player = (GameObject)GameObject.Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
    24.         GameObject parent = GameObject.Find("Hull");
    25.         player.transform.parent = parent.transform;
    26.  
    27.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    28.     }
    29.  
    30.     public override void OnServerReady(NetworkConnection conn)
    31.     {
    32.         base.OnServerReady(conn);
    33.  
    34.         ClientScene.RegisterPrefab(bulletPrefab);
    35.     }
    36. }
    Network Manager object:
    NetworkManager.PNG

    When I run the game through the editor and select Host in the NetworkManagerHUD, a player character is no longer spawned. Through breakpoints, I can tell that I need the Auto Create Player option checked to run the OnServerAddPlayer() method. How should I be doing this? I'd like to have full control over spawning players where and when I want.
     
  2. peterept2

    peterept2

    Joined:
    Aug 1, 2012
    Posts:
    41
    To add a player, once you are connected call:

    ClientScene.AddPlayer(0);
     
  3. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Does that mean, inside OnServerReady? (I'm having a hard time determining the structure required to set up custom UNet functionality in general. I read the manual, but it's quite sparse once you want to try expanding on the provided high-level features.)
     
  4. peterept2

    peterept2

    Joined:
    Aug 1, 2012
    Posts:
    41
    No, you want to override OnClientConnect() which is called when the client makes a connection to the server.

    The default implementation is right now:

    Code (CSharp):
    1. public virtual void OnClientConnect (NetworkConnection conn)
    2. {
    3.     if (string.IsNullOrEmpty (this.m_OnlineScene) || this.m_OnlineScene == this.m_OfflineScene)
    4.     {
    5.         ClientScene.Ready (conn);
    6.         if (this.m_AutoCreatePlayer)
    7.         {
    8.             ClientScene.AddPlayer (0);
    9.         }
    10.     }
    11. }
     
  5. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Ok thanks, I'll give that a try. Just for understanding what I'm doing...for a player-per-client AddPlayer(x) would always be 0, and split screen could be done adding players 0 and 1?
     
  6. peterept

    peterept

    Joined:
    Dec 6, 2012
    Posts:
    22
    yeah, so far that's how I see it.
     
  7. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Ok, great. Btw, looks like they changed the default code so for anyone who comes later, look here for the latest: http://docs.unity3d.com/Manual/UNetManager.html.

    So that just adds a new player entity. How do I spawn their controller so they have isLocalPlayer authority? Is that where NetworkServer.AddPlayerForConnection(conn, player, playerControllerId) comes in, and I'd use playerControllerId = 0?

    Edit: Looking through the docs, maybe OnServerAddPlayer is called when I use ClientScene.AddPlayer()? If so can I leave my OnServerAddPlayer() as is, or do I need to use AddPlayerForConnection() inside the OnClientConnect()?

    Right now I'm running into a spawn error. The host/server start fine, and I can control my player. On the client start, launched through the NetworkManagerHUD on a built instance, I get an error of "Spawn scene object not found for 4".
     
    Last edited: Jul 16, 2015
  8. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Bump. Still can't figure this out :/
     
  9. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Try posting your codes you have right now.
     
  10. Deleted User

    Deleted User

    Guest

    Code (csharp):
    1.  
    2. void SpawnPlayer()
    3. {
    4.      if(!NetworkManager.singleton.client.ready)
    5.      {
    6.           NetworkManager.singleton.client.Ready();
    7.      }
    8.      ClientScene.AddPlayer(0);
    9. }
    10.  
    Disable auto spawn player in the network manager.
    Then call SpawnPlayer(); once youre connected to the server.