Multiplayer Unity 3D With Azure: Hooking It All Up and Making It Work
Introduction
In our previous articles, we set up Photon PUN with Azure and Unity 3D to create a real-time multiplayer game. Now, it’s time to hook everything up and make it work seamlessly! 🎮
We’ll cover:
- User authentication (sign-up/sign-in)
- Lobby mechanics (how players join and interact)
- Game session handling (starting and managing games)
- Smooth player movement (interpolation, prediction, and lag compensation)
- Photon’s different networking update models
- Anti-cheat strategies and Photon’s built-in protections
Step 7: User Authentication (Sign-Up & Sign-In)
7.1 Setting Up PlayFab Authentication with Photon
We’ll use Azure PlayFab to manage user authentication before connecting to Photon.
Step 1: Initialize PlayFab in Unity
Install the PlayFab SDK via the Unity Package Manager and create a PlayFabManager.cs script:
|
|
Step 2: Connect PlayFab to Photon
Once authenticated, we need to connect Photon to PlayFab:
|
|
Now, users authenticate with PlayFab before joining a Photon multiplayer session. ✅
Step 8: The Lobby System
A lobby is where players wait before joining a game. Photon supports:
- Default Lobby - A simple list of open game rooms.
- Custom Matchmaking Lobbies - Filtered searches (e.g., rank-based matchmaking).
8.1 Creating a Lobby UI
We need a UI panel listing available rooms. Here’s how we fetch room lists:
|
|
This updates the lobby UI dynamically as rooms are created.
Step 9: Handling Game Start
Once players are in the lobby, we let them start a game:
|
|
Step 10: Smooth Player Movement and Position Updates
Photon provides multiple ways to synchronize player movement:
- Photon Transform View (simple but laggy)
- Manual Synchronization (more control, less lag)
- State Interpolation & Prediction (best for smoothness)
10.1 Using Photon Transform View (Easy Mode)
|
|
10.2 Advanced: Lag Compensation & Prediction
For better smoothness, use interpolation:
|
|
Step 11: Anti-Cheat Strategies in Photon
Photon does not have built-in cheat detection but allows:
- Server-side authoritative movement
- Encryption for network packets
- Custom anti-cheat logic
Pros and Cons of Photon Anti-Cheat
Feature | Pros | Cons |
---|---|---|
Client-side checks | Easy to implement | Hackers can bypass |
Server-authoritative | Harder to hack | More server load |
Encrypted packets | Prevents packet injection | Not bulletproof |
NOTE
Unity3d is very versatile and can target many platforms. All the techniques here apply to iPhone-iOS, Android , as well as Windows and Mac.
Unity3d has the ability to target web pages- by compiling to web assembly. BUT the caveat is when you are running on a web page, your code can talk back to the server it came from , but cannot make calls out to other domains (cross origin issues).