Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Multiplayer connection approval behaviour

Discussion in 'Getting Started' started by seandoc77, Aug 5, 2023.

  1. seandoc77

    seandoc77

    Joined:
    Jun 12, 2019
    Posts:
    2
    I'm just getting started with netcode for gameobjects and set up a simple connection approval script that handles player spawning. I placed empty game objects throughout the scene with a "SpawnPoint" tag. The script randomly chooses the spawn point and assigns that position to the connecting client's spawn position. This works when clients connect, but does not work when the host starts it. The host always ends up spawning at 0,0,0. Any idea why this is?

    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Netcode;
    3. public class ConnectionApprovalHandler : MonoBehaviour
    4. {
    5.     private const int MaxPlayers = 10;
    6.     [SerializeField] private GameObject[] spawnPoints;
    7.  
    8.     private Vector3 getSpawnPoint()
    9.     {
    10.         spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");
    11.         return spawnPoints[Random.Range(0, spawnPoints.Length - 1)].transform.position;
    12.     }
    13.  
    14.     private void Start()
    15.     {
    16.         NetworkManager.Singleton.ConnectionApprovalCallback = ApprovalCheck;
    17.     }
    18.     private void ApprovalCheck(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
    19.     {
    20.         Debug.Log("Connect Approval");
    21.         response.Approved = true;    
    22.         response.CreatePlayerObject = true;
    23.         response.PlayerPrefabHash = null;
    24.         response.Position = getSpawnPoint();
    25.                                      
    26.         if(NetworkManager.Singleton.ConnectedClients.Count >= MaxPlayers)
    27.         {                            
    28.             response.Approved = false;
    29.             response.Reason = "Server is full.";
    30.         }
    31.  
    32.         response.Pending = false;
    33.     }
    34. }
     
  2. PlayboyMinimi

    PlayboyMinimi

    Joined:
    Jul 15, 2018
    Posts:
    2
    I have the same problem.

    I start a server and when i connect with the first client he is always spawning on position 0,0,0.

    The second client(player) is spawning at a correct spawning point. It seems that always the first player ends up on position 0,0,0....

    Is there a solution for that???
     
  3. illeinvest

    illeinvest

    Joined:
    Nov 1, 2023
    Posts:
    2
    This string should be changed tunnel rush
    private void ApprovalCheck(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
    {
    Debug.Log("Connect Approval");
     
  4. codercurtis

    codercurtis

    Joined:
    Jan 19, 2016
    Posts:
    4
    I also have this problem. I have no idea what @illeinvest is trying to say.
     
  5. rafhaelxd

    rafhaelxd

    Joined:
    Oct 1, 2022
    Posts:
    2
    Why dont use an framework like fishnet to do it for you?
     
  6. codercurtis

    codercurtis

    Joined:
    Jan 19, 2016
    Posts:
    4