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. Dismiss Notice

Question Client Disconnect on VR freezes the other clients

Discussion in 'Netcode for GameObjects' started by marzdh, Aug 27, 2023.

  1. marzdh

    marzdh

    Joined:
    Jun 3, 2013
    Posts:
    31
    Hi,

    The players enter a VR experience either as a host or client. The players are being spawned using the Player Prefab defined in the Network Manager. So when I begin the script of StartHost() or StartClient(), the player is taken to the next scene where the player spawns. All VR interactions in the new scene work well.

    But when any player leaves the experience, the controllers for the rest of the players freezes. I'm not sure why this is happening. Any help will be appreciated.

    I'm handling the player disconnect using the script below

    Code (CSharp):
    1. private void Singleton_OnClientDisconnectCallback(ulong id)
    2.     {
    3.         if (IsServer)
    4.         {
    5.             playersInGame.Value--;
    6.             Debug.Log($"Id disconnected {id} and players count is {playersInGame.Value}");
    7.            
    8.         }
    9.         else
    10.         {
    11.             NetworkManager.Singleton.DisconnectClient(id);
    12.         }
    13.  
    14.     }
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,393
    Debug IsServer to check for false positives and share the DisconnectClient code
     
  3. marzdh

    marzdh

    Joined:
    Jun 3, 2013
    Posts:
    31
    So I've changed my code a bit. When any client disconnects, I'm calling a Client RPC.

    Code (CSharp):
    1. private void Singleton_OnClientDisconnectCallback(ulong obj)
    2.     {
    3.         Debug.Log($"Client ID disconnected {obj}");
    4.         if (IsServer)
    5.         {
    6.             RequestShutdownClientRpc(obj);
    7.     }
    8. }
    9.  
    10. [ClientRpc]
    11.     public void RequestShutdownClientRpc(ulong id)
    12.     {
    13.         LobbyInGame.instance.OnDestroyLobby(id);
    14.     }
    15.  
    LobbyInGame is a component on a game object in the scene, which creates Lobbies. I'm doing the shutdown for that particular client who has exited.

    Code (CSharp):
    1. public async void OnDestroyLobby(ulong id)
    2.     {
    3.         try
    4.         {
    5.             string playerId = AuthenticationService.Instance.PlayerId;
    6.             if(id.ToString() == playerId)
    7.             {
    8.                 StopCoroutine(heartbeat);
    9.                 if (joinedLobby != null)
    10.                 {
    11.                     if (joinedLobby.HostId == playerId)
    12.                     {
    13.                         await Lobbies.Instance.DeleteLobbyAsync(joinedLobby.Id);
    14.                     }
    15.                     else
    16.                     {
    17.                         await Lobbies.Instance.RemovePlayerAsync(joinedLobby.Id, playerId);
    18.                         NetworkManager.Singleton.Shutdown();
    19.                     }
    20.                 }
    21.                 Debug.Log("Joined Lobby now has " + joinedLobby.Players.Count);
    22.             }
    23.         }
    24.         catch (Exception e)
    25.         {
    26.             Debug.Log($"Error shutting down lobby: {e}");
    27.         }
    28.     }
    The controllers still freeze for all remaining clients. I don't want that to happen. When a client leaves, I want the others to continue playing.
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    223
    When is Singleton_OnClientDisconnectCallback called?
     
  5. marzdh

    marzdh

    Joined:
    Jun 3, 2013
    Posts:
    31
    The Client Disconnect code is attached to every client. So when a client leaves or closes the app, the callback is called.
     
  6. marzdh

    marzdh

    Joined:
    Jun 3, 2013
    Posts:
    31
    Can anyone help me with this issue of the controllers freezing for everyone when a client disconnects?

    I have reduced my code to contain the bare minimum logic with 1 host and one client connecting with Netcode.Singleton.StartHost() and Netcode.Singleton.StartClient().

    I have one teleportation area for the player to move around. There is no lobby and relay anymore.
    I've attached an image showing the components attached to my prefab, which is later spawned on the network.

    Yet when the client exits, the controllers of the host freeze. All animations in the background continue to play.
     

    Attached Files:

  7. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    603
    How do you read the input from the controllers, are you able to log it do see if it's still sending?