Back to Game Development

Module 7: Multiplayer & Networking

Master client-server architecture, Photon Unity Networking, and build multiplayer games

🌐 What is Multiplayer Gaming?

Multiplayer games let players interact in the same game world, even when they're on different computers! It's one of the most exciting (and challenging) aspects of game development. From co-op adventures to competitive shooters, multiplayer creates unforgettable experiences!

Simple Definition

Multiplayer Networking is synchronizing game state across multiple devices. When one player moves, everyone else sees it. When someone shoots, everyone hears it. It's like magic, but it's actually clever programming!

Multiplayer challenges:

• Synchronizing game state

• Handling network lag

• Preventing cheating

• Managing connections

• Scaling to many players

Why Learn Multiplayer?

Player Engagement

Multiplayer games have higher retention and replayability

Social Experience

Playing with friends creates memorable moments

Competitive Scene

Esports and competitive gaming are huge industries

Valuable Skill

Networking knowledge applies beyond games

📚 Learn More:

🖥️ Client-Server Architecture

Most multiplayer games use client-server architecture. The server is the authority - it decides what's real. Clients (players) send inputs to the server, and the server sends back the game state. This prevents cheating and keeps everyone synchronized!

Architecture Types

Client-Server (Authoritative)

Pros: Secure, prevents cheating, scalable

Cons: Requires server hosting, more complex

Best for: Competitive games, MMOs, shooters

Peer-to-Peer (P2P)

Pros: No server needed, lower latency

Cons: Vulnerable to cheating, harder to scale

Best for: Co-op games, small player counts

How It Works

1. Client sends input: "I pressed W key"

2. Server processes: "Player moved forward 5 units"

3. Server broadcasts: "Player 1 is now at position (10, 0, 5)"

4. All clients update: Show Player 1 at new position

This happens 20-60 times per second!

⚡ Photon Unity Networking (PUN)

Photon is the most popular networking solution for Unity! It handles the complex networking code, provides free hosting for small games, and has excellent documentation. Perfect for beginners and professionals alike!

Setting Up Photon

Step 1: Get Photon

1. Go to photonengine.com

2. Create free account

3. Create new PUN app, copy App ID

4. In Unity: Window → Asset Store → Search "PUN 2 FREE"

5. Download and import

6. Paste App ID when prompted

Basic Photon Code

// NetworkManager.cs - Connect to Photon

using Photon.Pun;

using Photon.Realtime;

public class NetworkManager : MonoBehaviourPunCallbacks

{

void Start()

{

// Connect to Photon servers

PhotonNetwork.ConnectUsingSettings();

}

public override void OnConnectedToMaster()

{

Debug.Log("Connected to Photon!");

PhotonNetwork.JoinLobby();

}

public override void OnJoinedLobby()

{

Debug.Log("Joined lobby!");

}

public void CreateRoom()

{

RoomOptions options = new RoomOptions();

options.MaxPlayers = 4;

PhotonNetwork.CreateRoom("MyRoom", options);

}

public void JoinRoom()

{

PhotonNetwork.JoinRoom("MyRoom");

}

public override void OnJoinedRoom()

{

Debug.Log("Joined room!");

// Spawn player

PhotonNetwork.Instantiate("PlayerPrefab", Vector3.zero, Quaternion.identity);

}

}

Synchronizing Objects

// PlayerController.cs - Networked player

using Photon.Pun;

public class PlayerController : MonoBehaviourPun

{

public float speed = 5f;

void Update()

{

// Only control YOUR player

if (!photonView.IsMine) return;

float h = Input.GetAxis("Horizontal");

float v = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(h, 0, v) * speed * Time.deltaTime;

transform.Translate(movement);

}

// Call this to sync an action

public void Shoot()

{

if (!photonView.IsMine) return;

// Call RPC on all clients

photonView.RPC("ShootRPC", RpcTarget.All);

}

[PunRPC]

void ShootRPC()

{

// This runs on ALL clients

Debug.Log("Player shot!");

// Play shoot animation, sound, etc.

}

}

🔧 Netcode Fundamentals

Netcode is the code that handles networking. Good netcode makes multiplayer feel smooth even with lag. Let's learn the key concepts!

Key Networking Concepts

RPCs (Remote Procedure Calls)

Call a function on other players' games. Perfect for events like shooting, jumping, or chatting. Use sparingly - too many RPCs can lag the game!

State Synchronization

Automatically sync variables across network. Position, health, score - mark them for sync and Photon handles the rest! Use PhotonTransformView for position/rotation.

Ownership

Each networked object has an owner. Only the owner can control it. Check photonView.IsMine before accepting input!

Instantiation

Use PhotonNetwork.Instantiate() instead of Instantiate(). This spawns the object on all clients. Prefabs must be in Resources folder!

⏱️ Lag Compensation

Network lag is inevitable - data takes time to travel! Good netcode hides lag through clever techniques. Let's learn how to make multiplayer feel smooth!

Lag Compensation Techniques

Client-Side Prediction

Player moves immediately on their screen, then server confirms. If wrong, snap to correct position. Makes controls feel instant!

Interpolation

Smooth movement between network updates. Instead of teleporting, objects glide smoothly. Essential for other players' movement!

Lag Compensation

Server rewinds time when checking hits. If you shot where enemy WAS on your screen, it counts! Makes shooting feel fair despite lag.

Dead Reckoning

Predict where objects will be based on velocity. If updates stop, keep moving in last direction. Reduces visible lag!

💡 Networking Best Practices:

  • • Send only what's necessary - bandwidth is limited!
  • • Use delta compression - send only what changed
  • • Prioritize important data (player position > cosmetics)
  • • Test with artificial lag to find issues early
  • • Always validate on server - never trust clients!

🎯 Complete Example: Multiplayer Game

You now have all the knowledge to build multiplayer games! Start simple - a basic multiplayer movement game. Then add features: shooting, scoring, chat, matchmaking. The possibilities are endless!

🎓 What You've Learned:

  • • Client-server architecture fundamentals
  • • Photon Unity Networking setup and usage
  • • RPCs and state synchronization
  • • Lag compensation techniques
  • • Best practices for multiplayer development
  • • Complete foundation for building multiplayer games!

📚 Learning Resources

Networking

Advanced Topics

🎉 Congratulations!

You've completed the Game Development course! You now have the skills to create amazing games - from fundamentals to multiplayer. Keep practicing, keep building, and most importantly, keep having fun! The game development journey never ends - there's always something new to learn and create!