#```[Thread with the whole script]```
1 messages ยท Page 1 of 1 (latest)
// Create a new lobby with P2P first, Relay only if needed
public async void OnCreateLobby()
{
CloseLobbyMenu(true);
_connectionTypeText.text = "";
_connectionTypeText.color = new(1f, 0.5f, 0f);
try
{
var options = new SessionOptions
{
MaxPlayers = 4
};
bool canUseDirectP2P = await TryDirectP2P();
if (canUseDirectP2P)
{
_connectionTypeText.text = $"P2P Server";
}
else
{
Debug.Log("Direct P2P failed, enabling Relay.");
_connectionTypeText.text = $"Relay Server";
options.WithRelayNetwork(); // Enable Relay as fallback
}
var session = await MultiplayerService.Instance.CreateSessionAsync(options);
_currentSession = session;
NetworkManager.Singleton.StartHost();
DisplayLobbyCode(session.Code);
CloseLobbyMenu(true);
}
catch (System.Exception e)
{
_connectionTypeText.text = $"Error Creating a Room";
_connectionTypeText.color = Color.red;
_createLobbyButton.interactable = true;
_openLobbyMenuButton.interactable = true;
}
}```
async void OnJoinLobby()
{
_joinLobbyButton.interactable = false;
string enteredCode = _lobbyCodeInput.text;
_errorText.text = "";
_connectionTypeText.text = $"Joining Room";
try
{
// Join the session using the code
var session = await MultiplayerService.Instance.JoinSessionByCodeAsync(enteredCode);
if (session == null)
{
_errorText.text = "Session not found.";
return;
}
_currentSession = session;
NetworkManager.Singleton.StartClient();
// Successfully connected, clear the UI
_connectionTypeText.text = "";
CloseLobbyMenu(true);
DisplayLobbyCode(session.Code);
}
catch (System.Exception e)
{
_errorText.text = "Error joining session.";
_joinLobbyButton.interactable = true;
}
}
void DisplayLobbyCode(string code)
{
_lobbyCode.text = code;
_createLobbyButton.interactable = false;
_openLobbyMenuButton.interactable = false;
_joinLobbyButton.interactable = false;
}
}```
async Task<bool> TryDirectP2P()
{
Debug.Log("๐ Checking NAT type and attempting direct P2P connection...");
try
{
// Step 1: Get local IP
string localIP = GetLocalIPAddress();
Debug.Log($"Local IP: {localIP}");
// Step 2: Get public IP
string publicIP = await GetPublicIPAddress();
Debug.Log($"Public IP: {publicIP}");
_ipText.text = publicIP; // Display on UI
// Step 3: Check UDP connectivity (testing if ports are open)
bool udpWorking = await TestUDPConnectivity();
if (!udpWorking)
{
Debug.Log("UDP Connection Failed - Likely behind CGNAT");
return false;
}
Debug.Log("Open NAT - P2P should work");
return true;
}
catch (System.Exception e)
{
Debug.LogError($"P2P Test Failed: {e.Message}");
return false;
}
}
async Task<string> GetPublicIPAddress()
{
using (HttpClient client = new HttpClient())
{
try
{
// Force IPv4-only address retrieval
string ipv4 = await client.GetStringAsync("https://api.ipify.org?format=text");
Debug.Log($"Public IP (IPv4): {ipv4}");
return ipv4;
}
catch (Exception ex)
{
Debug.LogError($"Error retrieving IPv4 address: {ex.Message}");
return "Unknown";
}
}
}```
// Test UDP connectivity with specific port and more detailed logging
async Task<bool> TestUDPConnectivity()
{
try
{
using (UdpClient udpClient = new UdpClient())
{
// Test connection to Google's DNS server and Unity's default P2P port
udpClient.Connect("8.8.8.8", 7777); // Use port 7777, which is commonly used for Unity P2P
await Task.Delay(100); // Simulate network delay for realistic results
IPEndPoint endPoint = (IPEndPoint)udpClient.Client.LocalEndPoint;
string udpTestIP = endPoint.Address.ToString();
Debug.Log($"UDP Test IP: {udpTestIP}");
// Return true if the connection is successful
return true;
}
}
catch (Exception ex)
{
Debug.LogError($"UDP Test Failed: {ex.Message}");
return false;
}
}
string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork) // IPv4
{
return ip.ToString();
}
}
throw new System.Exception("No network adapters with an IPv4 address found!");
}```
You are not getting any errors. Looks like the client connected to the session.
Do you have the network manager spawning the player objects?
yes
well with relay it works
with this it does not spawn it for the clients, only for the host
what I do get is the error sending 1 packets, constantly on the console for the client
I did get an error
oh that's a transport error. Are you using VPN or anything?