CHAPTER 16
Beginner
Multiplayer and Networking Basics
Updated: May 16, 2026
35 min read
# CHAPTER 16
Multiplayer and Networking Basics
1. Introduction
Building a single-player game is challenging. Building a multiplayer game is an entirely different paradigm of computer science. When Player A moves, how does Player B's computer know about it? How do you stop players from cheating? How do you handle internet lag? In this chapter, we will master Multiplayer and Networking Basics. We will explore the fundamental Client-Server architecture, introduce Unity's official networking solution (Netcode for GameObjects), and learn how to synchronize player positions and execute remote commands over the internet.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the Host/Server/Client networking architecture.
- Understand the concept of "Server Authority" to prevent cheating.
- Install Unity's Netcode for GameObjects (NGO).
- Synchronize a Player's Transform across multiple computers.
- Understand RPCs (Remote Procedure Calls) for network communication.
3. Client-Server Architecture
In multiplayer games, computers must talk to each other.- The Client: This is the player playing the game on their home PC. They send inputs ("I pressed jump") to the Server.
- The Server: The central dictator. It receives inputs, calculates the physics, and tells all Clients exactly what happened.
- The Host: In indie games, having dedicated servers is expensive. Instead, one player acts as both a Client *and* the Server simultaneously. This is called "Hosting."
4. Server Authority (Anti-Cheat)
If a Client says, *"I just found 1000 gold,"* the Server must reject it. Never trust the Client.- The Client only says: *"I clicked my mouse."*
- The Server says: *"You clicked, therefore you swung your sword, therefore you hit the monster, therefore the monster died, therefore 1000 gold drops."*
5. Netcode for GameObjects (NGO)
Unity's official multiplayer package is called Netcode for GameObjects. To use it, you must install it via the Package Manager. Instead of inheriting fromMonoBehaviour, your multiplayer scripts must inherit from NetworkBehaviour.
csharp
6. Synchronizing Movement (Network Transform)
If Player A moves, Player B needs to see the movement. Instead of writing complex C# networking code, Unity provides a built-in Component:- 1. Attach a NetworkObject component to your Player Prefab.
- 2. Attach a NetworkTransform component to your Player Prefab.
7. RPCs (Remote Procedure Calls)
How do you tell the Server that you shot a gun, so the Server can spawn the bullet for everyone to see? You use an RPC.- ServerRpc: A message sent from a Client to the Server. ("I am asking to shoot.")
- ClientRpc: A message sent from the Server to all Clients. ("Everyone, play a gunshot sound!")
csharp
8. Visual Learning: The Multiplayer Pipeline
txt
9. Best Practices
-
NetworkManager Setup: Every multiplayer game requires an empty GameObject with a
NetworkManagercomponent. You must define a Transport (like Unity Transport) which dictates the underlying UDP/TCP network protocols. You must also register your Player Prefab inside the NetworkManager so it knows what to spawn when someone joins!
10. Common Mistakes
- Syncing Everything: Beginners try to sync the exact rotation of every blade of grass blowing in the wind. This will instantly crash your network bandwidth. Only synchronize objects that actually affect gameplay (Players, Enemies, Bullets). Visual fluff should be calculated locally on each client's machine.
11. Mini Project: Setup a Networked Player
Objective: Prepare a player prefab for internet synchronization.- 1. Create a Capsule. Name it "NetworkPlayer".
-
2.
Add a
NetworkObjectcomponent.
-
3.
Add a
NetworkTransformcomponent.
-
4.
Add the
PlayerMultiplayer.csscript (from Section 5).
- 5. Drag the "NetworkPlayer" into your Project Window to make it a Prefab. Delete it from the scene.
-
6.
Create an Empty GameObject named
NetworkManager. Add theNetworkManagercomponent.
-
7.
Set the Transport to
Unity Transport.
-
8.
Drag your "NetworkPlayer" prefab into the
Player Prefabslot on the NetworkManager.
- 9. When you click "Start Host" in your UI, Unity will automatically spawn your networked player into the world!
12. Practice Exercises
- 1. What specific C# base class must your scripts inherit from to access Unity's Netcode functionality?
- 2. If you want a visual effect (like an explosion) to happen on every player's screen simultaneously, what type of RPC should the Server call?
13. MCQs with Answers
Question 1
In a multiplayer Unity game, which component automatically handles synchronizing an object's X/Y/Z position across the internet so other players can see it move?
Question 2
To prevent hackers from cheating, games use "Server Authority." In this architecture, which computer is actually responsible for calculating the physics of a bullet hitting an enemy?
14. Interview Questions
-
Q: Explain the mechanical difference between a
ServerRpcand aClientRpcin Unity's Netcode for GameObjects. Walk through a scenario utilizing both to throw a grenade.
-
Q: Describe the concept of
IsOwnerin a multiplayer script. Why is checkingif (!IsOwner) return;the absolute most critical line of code in a multiplayer movement script?
- Q: Contrast a dedicated Server architecture with a Host/Client architecture. What are the financial and gameplay tradeoffs of relying on players to Host games?
15. FAQs
Q: Is multiplayer too hard for a beginner? A: Yes, it is notoriously difficult. If you are building your very first Unity game, do NOT make it multiplayer. Learn how to build a complete, polished single-player game first, then tackle multiplayer in your second or third project.16. Summary
In Chapter 16, we expanded our game from a local experience into a worldwide connected lobby. We learned the fundamental Client-Server architecture and the absolute necessity of Server Authority to prevent cheating. We utilized Unity's Netcode for GameObjects, replacingMonoBehaviour with NetworkBehaviour. We protected our movement using IsOwner, synchronized transforms automatically, and fired custom network events using ServerRpc and ClientRpc.