Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved Getting clients to find host on other computer/next steps for netcode and connecting

Discussion in 'Multiplayer' started by nitrogendragon, Jan 29, 2022.

  1. nitrogendragon

    nitrogendragon

    Joined:
    Sep 13, 2017
    Posts:
    6
    So I followed the dapper dino tutorial for netcode awhile back and have been building on top of that to make a 2 player card game and have been testing with multiple instances on my computer and got it to a playable state.
    My issue now is I created an executable, sent it to another computer and tried getting both players to connect to test the game but the client (player 2) does not seem to be able to find the other player(player 1 aka host) in the same manner that it was able to when running on my computer.

    So my question is in general what is the next step in getting the players to be able to find one another? Is it something to do with network firewalls just blocking access or is there something beyond netcode that I need to integrate now in order to overcome this final hurdle? Any help is appreciated and let me know if there is anything additional that I should post.

    Thanks in advance.

    This is my current main script for handling the network setup stuff

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    using Unity.Netcode;
    using System.Text;
    using UnityEngine.SceneManagement;

    public class PasswordNetworkManager : MonoBehaviour
    {

    [SerializeField] private GameObject passwordInputField;
    [SerializeField] private GameObject playerIconPickerUI;
    [SerializeField] private GameObject passwordEntryUI;
    [SerializeField] private GameObject leaveButton;
    [SerializeField] private GameObject playerCanvas;
    [SerializeField] private GameObject uiMain;



    private static ulong hostClientId;//our host client
    private static ulong player2ClientId;// our connected client aka player 2


    private void Start()
    {
    NetworkManager.Singleton.OnServerStarted += HandleServerStarted;
    NetworkManager.Singleton.OnClientConnectedCallback += HandleClientConnected;
    NetworkManager.Singleton.OnClientDisconnectCallback += HandleClientDisconnect;

    }

    private void Update()
    {
    if (NetworkManager.Singleton.IsHost && uiMain.GetComponent<LoadingAndWaitingScreen>().GetCurrentMessage(2) && NetworkManager.Singleton.ConnectedClients.Count == 1)
    {
    uiMain.GetComponent<LoadingAndWaitingScreen>().SetScreenMessage(1);
    }
    }

    private void OnDestroy()
    {

    // Prevent error in the editor
    if (NetworkManager.Singleton == null) { return; }

    NetworkManager.Singleton.OnServerStarted -= HandleServerStarted;
    NetworkManager.Singleton.OnClientConnectedCallback -= HandleClientConnected;
    NetworkManager.Singleton.OnClientDisconnectCallback -= HandleClientDisconnect;
    }

    public void Host()
    {
    // Hook up password approval check
    NetworkManager.Singleton.ConnectionApprovalCallback += ApprovalCheck;
    NetworkManager.Singleton.StartHost();

    }

    public void Client()
    {
    // Set password ready to send to the server to validate
    NetworkManager.Singleton.NetworkConfig.ConnectionData = Encoding.ASCII.GetBytes(passwordInputField.GetComponent<TMP_InputField>().text);
    NetworkManager.Singleton.StartClient();

    }



    public void Leave()
    {

    ShutdownEveryone();

    }

    //[ServerRpc(RequireOwnership =false)]
    private void ShutdownEveryone()
    {

    ShutdownClient();
    }


    private void ShutdownClient()
    {

    NetworkManager.Singleton.Shutdown();
    NetworkManager.Singleton.ConnectionApprovalCallback -= ApprovalCheck;
    //}
    //Debug.Log("The host or a client is setting active states");

    //everyone does this clean up
    //passwordEntryUI.SetActive(true);
    //playerIconPickerUI.SetActive(false);
    //leaveButton.SetActive(false);
    //playerCanvas.SetActive(false);
    SceneManager.LoadScene("TriTriOnline");
    }

    private void HandleServerStarted()
    {
    // Temporary workaround to treat host as client
    //if (NetworkManager.Singleton.IsHost)
    //{
    // //HandleClientConnected(NetworkManager.Singleton.ServerClientId);
    //}
    }

    private void HandleClientConnected(ulong clientId)
    {
    // Are we the client that is connecting?
    if (clientId == NetworkManager.Singleton.LocalClientId)
    {

    passwordEntryUI.SetActive(false);
    leaveButton.SetActive(true);
    playerIconPickerUI.SetActive(true);
    playerCanvas.SetActive(true);

    //If we are the host we want to set our screen active and text to waiting text
    if (NetworkManager.Singleton.IsHost)
    {
    //Debug.Log("we ran host screen check");
    //set waiting for player message for host
    uiMain.GetComponent<LoadingAndWaitingScreen>().SetScreenMessage(1);
    //no reason for this to fail that i can think of

    hostClientId = NetworkManager.Singleton.LocalClientId;
    return;
    }

    //if we are the client we want to set all screens to prepping message texxt
    if (NetworkManager.Singleton.IsClient)
    {

    //Debug.Log("we ran the client check for screens");
    //player prep message for player2
    uiMain.GetComponent<LoadingAndWaitingScreen>().SetScreenMessage(2);
    //player prep message for host
    uiMain.GetComponent<LoadingAndWaitingScreen>().AdjustClientScreenMessageServerRpc(2, hostClientId);

    player2ClientId = NetworkManager.Singleton.LocalClientId;
    }



    }


    }

    private void HandleClientDisconnect(ulong clientId)
    {

    // Are we the client that is disconnecting?
    if (clientId == NetworkManager.Singleton.LocalClientId)
    {
    //Leave();
    //passwordEntryUI.SetActive(true);
    //leaveButton.SetActive(false);
    //playerIconPickerUI.SetActive(false);
    //playerCanvas.SetActive(false);
    }
    }

    public void HandlePlayerReadyUp(bool isReady)
    {
    if (isReady)
    {
    //we don't want to be picking our team/updating playericon anymore
    playerIconPickerUI.SetActive(false);
    return;
    }
    //otw we aren't ready so we can pick our icons again
    playerIconPickerUI.SetActive(true);
    }



    private void ApprovalCheck(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate callback)
    {
    string password = Encoding.ASCII.GetString(connectionData);

    bool approveConnection = password == passwordInputField.GetComponent<TMP_InputField>().text;

    Vector3 spawnPos = Vector3.zero;
    Quaternion spawnRot = Quaternion.identity;

    switch (NetworkManager.Singleton.ConnectedClients.Count)
    {
    case 0:
    spawnPos = new Vector3(-12f, -8f, 0f);
    spawnRot = Quaternion.Euler(0f, 0f, 0f);
    break;
    case 1:
    spawnPos = new Vector3(12f, -8f, 0f);
    spawnRot = Quaternion.Euler(0f, 0f, 0f);
    break;
    //case 2:
    // spawnPos = new Vector3(2f, 0f, 0f);
    // spawnRot = Quaternion.Euler(0f, 0f, 0f);
    // break;
    }
    callback(true, null, approveConnection, spawnPos, spawnRot);
    }


    }
     
  2. nitrogendragon

    nitrogendragon

    Joined:
    Sep 13, 2017
    Posts:
    6
    Updated: Issue resolved, needed to set up a relay server for online multiplayer, didn't realize i only had local co-op set up
     
  3. Arti_Z

    Arti_Z

    Joined:
    Sep 24, 2021
    Posts:
    1
    where is this configured? and could you send screenshots? Thank you