Search Unity

Unity Netcode Spawnpoints

Discussion in 'Netcode for GameObjects' started by elliotbra, Jun 8, 2023.

  1. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    56
    Hi, does someone know how to customize the spawn point for players using Netcode's NetworkManager? I mean, there are 2 spawnpoints and 2 players. And the players should spawn on a random one.
     
  2. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    179
    Hi @elliotbra You should create manually PlayerPrefab. Only Server/Host should run this method.
    You can listen "NetworkManager.Singleton.OnClientStarted" event then run the "SpawnPlayerObject" method.
    I hope this little code helps you.

    Code (CSharp):
    1. public void SpawnPlayerObject(ulong joinedClientId)
    2. {
    3.             Transform newGameObject = Instantiate(m_PlayerPrefab, GetAvaiablePosition(), Quaternion.identity);
    4.             NetworkObject newNetworkObject = newGameObject.GetComponent<NetworkObject>();
    5.             newNetworkObject.SpawnAsPlayerObject(joinedClientId, true);
    6. }
     
    Riccardoric likes this.
  3. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @elliotbra , you need to use ConnectionApprovalCallback. There's an example in this unreleased sample called API Diorama

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Unity.Netcode.Samples.APIDiorama.RPC
    4. {
    5.     /// <summary>
    6.     /// Manages how a player will be spawned
    7.     /// </summary>
    8.     class PlayerSpawnManager : NetworkBehaviour
    9.     {
    10.         void Start()
    11.         {
    12.             NetworkManager.ConnectionApprovalCallback = ConnectionApprovalCallback;
    13.         }
    14.  
    15.         void ConnectionApprovalCallback(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
    16.         {
    17.             /* you can use this method in your project to customize one of more aspects of the player
    18.              * (I.E: its start position, its character) and to perform additional validation checks. */
    19.             response.Approved = true;
    20.             response.CreatePlayerObject = true;
    21.             response.Position = GetPlayerSpawnPosition();
    22.         }
    23.  
    24.         Vector3 GetPlayerSpawnPosition()
    25.         {
    26.             /*
    27.              * this is just an example, and you change this implementation to make players spawn on specific spawn points
    28.              * depending on other factors (I.E: player's team)
    29.              */
    30.             return new Vector3(Random.Range(-3, 3), 0, Random.Range(-3, 3));
    31.         }
    32.     }
    33. }
     
  4. codercurtis

    codercurtis

    Joined:
    Jan 19, 2016
    Posts:
    4
    @RikuTheFuffs-U This appears to only set the position for clients, not the host. The first object that spawns always ends up a Vector3.zero, and the second object that spawns gets the position set as intended.

    Do you know why this is?