Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Overriding OnLobbyServerCreateGamePlayer

Discussion in 'Multiplayer' started by wraxul, Apr 25, 2017.

  1. wraxul

    wraxul

    Joined:
    Dec 10, 2012
    Posts:
    12
    Hi, we're working on a game where some of the online game modes involve teams. We want each team member to spawn at the same NetworkStartPosition, so I wrote an override for OnLobbyServerCreateGamePlayer that checks the player's team and finds the corresponding start position to spawn them. To figure out which player is being created, I'm using the playerControllerId parameter. If I'm not mistaken, this is supposed to be different for each player, but it is always 0, so all players spawn at the host's team location regardless of the LobbyPlayer's team ID.

    Is this a bug or am I going about this incorrectly?

    Code (CSharp):
    1.  
    2. public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
    3.     {
    4.         GameObject myPlayer;
    5.         NetworkStartPosition[] spawnPositions = FindObjectsOfType<NetworkStartPosition>() as NetworkStartPosition[];
    6.         // If no start positions are found, return null
    7.         if (spawnPositions == null || spawnPositions.Length == 0)
    8.         {
    9.             Debug.Log("No start positions found");
    10.             return null;
    11.         }
    12.         // Is this a team game mode?
    13.         if (GameManager.Instance.m_LevelMode == GameManager.LevelMode.LEVEL_CAPTURE_FLAG)
    14.         {
    15.             // Find the matching start position
    16.             int team = ((LobbyPlayer)lobbySlots[playerControllerId]).m_TeamID;
    17.             int posIndex = playerControllerId % spawnPositions.Length;
    18.             for (int i = 0; i < spawnPositions.Length; ++i)
    19.             {
    20.                 if (spawnPositions[i].name.Contains("Team"+team.ToString()))
    21.                 {
    22.                     // playerControllerId is always 0
    23.                     Debug.Log("Team" + team.ToString() + " start position found for controller id " + playerControllerId.ToString() + ". Use index " + i.ToString());
    24.                     posIndex = i;
    25.                     break;
    26.                 }
    27.             }
    28.             // Slight offset so the players don't overlap one another
    29.             Vector3 offsetPosition = new Vector3(UnityEngine.Random.Range(-0.5f, 0.5f), 0.0f, 0.0f);
    30.             myPlayer = Instantiate(gamePlayerPrefab, spawnPositions[posIndex].transform.position + offsetPosition, Quaternion.identity) as GameObject;
    31.         }
    32.         // Not team based, spawn the players round robin style
    33.         else
    34.         {
    35.             myPlayer = Instantiate(gamePlayerPrefab, spawnPositions[playerControllerId % spawnPositions.Length].transform.position, Quaternion.identity) as GameObject;
    36.         }
    37.  
    38.         return myPlayer;
    39.     }
    40.