ODIN Transport for Mirror Networking
This guide shows how to use the ODIN Transport for Mirror Networking package to provide P2P networking in your Unity games. The ODIN Transport package includes a Transport implementation, allowing Mirror to send and receive data over the 4Players ODIN servers.
ODIN Transport is designed to work in combination with the ODIN Voice Chat service, but can be used without utilizing voice.
What is Mirror?
“Mirror is a system for building multiplayer capabilities for Unity games. It is built on top of the lower level transport real-time communication layer, and handles many of the common tasks that are required for multiplayer games.” Mirror Docs
Mirror takes care of essentials like synchronizing player positions, animations, health and other game variables. It instantiates and destroys game objects across the network and ensures that these objects are managed correctly across the connected clients. It also handles player connections and disconnects and networked scenes.
While Mirror addresses these aspects of networking, it does not handle the actual transmission of data. This is where a transport layer, like the ODIN Transport for Mirror Networking, comes into play.
ODIN’s Role:
Data Transmission: While Mirror organizes and prepares the data for transmission, ODIN is responsible for actually sending and receiving this data over the 4Players ODIN servers.
Scalability and Reliability: By leveraging 4Players servers, ODIN Transport ensures that your multiplayer games can handle a larger number of concurrent players and provide a stable connection.
Voice Chat Integration: When used in combination with ODIN Voice Chat, ODIN Transport seamlessly integrates all kinds of voice communication into your multiplayer games.
In essence, while Mirror lays the groundwork for creating a multiplayer game within Unity, ODIN Transport bridges the gap between the game’s logic and the actual data transmission.
Setup
- Install Mirror into your project using the Package Manager. Mirror is available from the Unity Asset Store.
- Download the latest ODIN Unity SDK .unitypackage from the Github Releases page and import it into your project.
- Download the latest ODIN Transport for Unity SDK .unitypackage from the Github Releases page and import it into your project
- Locate the
OdinManager_NetworkingVariant
prefab in the “Packages/4Players ODIN Transport for Mirror Networking/Prefabs” directory and drop it into your scene. - Select the
OdinManager
GameObject, click on theManage Access
button underClient Authentication
and generate an Access Key. For more information on Access Keys, take a look at our Understanding Access Keys chapter. - Attach a
NetworkManager
component to a GameObject in your Scene. - Attach a
Odin Transport
component to the same GameObject as theNetworkManager
- Drag the
Odin Transport
component into theTransport
slot on theNetworkManager
For a more in-depth setup guide for ODIN, take a look at our Getting Started with Unity chapter or dive into our Unity video tutorial series.
Connecting
ODIN Transport uses ODIN rooms to handle networking messages. This means instead of entering an IP Address to start or host a client, you provide a room name to the networkAddress
field of the NetworkManager
. Here’s how you can do it:
public class MinimalMirrorConnection : MonoBehaviour
{
[SerializeField] private string roomId = "default";
public void StartHost()
{
NetworkManager.singleton.networkAddress = roomId;
NetworkManager.singleton.StartHost();
}
public void Connect()
{
NetworkManager.singleton.networkAddress = roomId;
NetworkManager.singleton.StartClient();
}
}
Note: When going live with your game, ensure you use unique room names. Failing to do so will result in all players connecting to the same networking room.
Using the Positional Audio script
For proximity voice chat functionality in your game, it’s essential to link the incoming voice stream with the corresponding player’s avatar. The ODIN Transport for Mirror package offers a handy utility script called OdinPositionalAudio
to streamline this process.
Here’s how to use it:
- Attach the
OdinPositionalAudio
script to the Player-Prefab’s GameObject that has theNetworkIdentity
component. - Specify your desired proximity room name in the script.
- (optional)
Once these steps are completed, the OdinPositionalAudio
script will take care of the rest. When a player connects, the script automatically generates a combined room name using the given proximity room’s name and the networking room’s name. The combination looks like this:
public string GetFullPositionalRoomName()
{
return $"{positionalRoomName}-{GetNetworkingRoomName()}";
}
If you’re new to setting up your Player Prefab for multiplayer in Mirror, consult the Mirror Getting Started Guide for a comprehensive walkthrough.
What’s next?
We’ve shown you how to setup Mirror Networking to use ODIN as a Relay Transport. For even more information on Unity with Odin Voice Chat, check out our Discord and take a look at the following guides we’ve prepared for you: