In this lesson, you will learn how to analyze connection performance and select optimal Unity Relay regions to ensure low-latency gameplay for a globally distributed player base.
Understanding Latency in Relay
When using Unity Relay with Netcode for GameObjects, your traffic is routed through a relay server. If a player in London connects to a relay server based in Tokyo, the physical distance induces "propagation delay," resulting in a high ping. To minimize this, you must intelligently select the relay region closest to the majority of players in a session.
Analyzing and Selecting Regions
The Relay service provides the ability to specify a region during allocation. Before a match begins, you should ideally perform a latency check from the client to various available regions. By pinging the Relay endpoints, you can determine which region offers the lowest Round Trip Time (RTT).
When creating an allocation, you pass the RelayServerEndpoint or specify the region identifier:
// Requesting an allocation in a specific region
Allocation allocation = await RelayService.Instance.CreateAllocationAsync(maxConnections, "europe-west2");
// Using the allocation to configure the UnityTransport
var transport = NetworkManager.Singleton.GetComponent<UnityTransport>();
transport.SetRelayServerData(new RelayServerData(allocation, "dtls"));
Strategic Placement
The "optimal" choice isn't always the closest to a single player. In a multiplayer match, you must balance the latency across all participants. A common architectural pattern is to identify the "average" geographical center of the lobby members or to allow the session host (or matchmaking service) to request an allocation in a region that minimizes the maximum latency experienced by any single player in the group.
Monitoring tools within the Unity Gaming Services dashboard allow you to inspect these connections. If you observe players consistently reporting "jitter" or high RTT, analyze whether your lobby system is defaulting to a sub-optimal region, such as the host's local region rather than one centrally located between all participants.
Key Takeaway: By programmatically selecting the Relay region closest to the collective player group rather than relying on default assignments, you significantly reduce ping-induced desync and improve the responsiveness of your networked game.