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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Unity Netcode Spawnpoints

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

  1. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    45
    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:
    145
    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. }
     
  3. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    426
    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. }