Skip to main content
Unity 3D Basics – Complete Beginner to Advanced Guide
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."*
This architecture (Server Authority) is the only way to prevent hacking in competitive games.

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 from MonoBehaviour, your multiplayer scripts must inherit from NetworkBehaviour.
csharp
1234567891011121314
using Unity.Netcode; // Required for multiplayer!

public class PlayerMultiplayer : NetworkBehaviour
{
    void Update()
    {
        // IsOwner checks if THIS computer is the one controlling this specific character.
        // We do not want Player A's keyboard to move Player B's character!
        if (!IsOwner) return;

        // Normal movement code goes here
        transform.Translate(Vector3.forward * Time.deltaTime);
    }
}

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. 1. Attach a NetworkObject component to your Player Prefab.
  1. 2. Attach a NetworkTransform component to your Player Prefab.
This component automatically reads the player's position 30 times a second and sends it over the internet to everyone else in the lobby!

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
123456789101112131415161718192021222324
void Update()
{
    if (!IsOwner) return;

    if (Input.GetMouseButtonDown(0))
    {
        // 1. Tell the Server we want to shoot!
        RequestShootServerRpc(); 
    }
}

[ServerRpc]
void RequestShootServerRpc()
{
    // 2. The Server receives the request, verifies ammo, and tells ALL clients to play the effect.
    PlayShootEffectClientRpc();
}

[ClientRpc]
void PlayShootEffectClientRpc()
{
    // 3. This runs on EVERY computer in the lobby instantly.
    particleSystem.Play();
}

8. Visual Learning: The Multiplayer Pipeline

txt
1234567
[ Player A's PC (Client) ]       [ Player B's PC (Client) ]
           \                           /
       (Sends inputs)            (Receives visual updates)
             \                       /
              v                     v
              [ THE HOST SERVER ]
     (Calculates hit detection, spawns enemies)

9. Best Practices

  • NetworkManager Setup: Every multiplayer game requires an empty GameObject with a NetworkManager component. 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. 1. Create a Capsule. Name it "NetworkPlayer".
  1. 2. Add a NetworkObject component.
  1. 3. Add a NetworkTransform component.
  1. 4. Add the PlayerMultiplayer.cs script (from Section 5).
  1. 5. Drag the "NetworkPlayer" into your Project Window to make it a Prefab. Delete it from the scene.
  1. 6. Create an Empty GameObject named NetworkManager. Add the NetworkManager component.
  1. 7. Set the Transport to Unity Transport.
  1. 8. Drag your "NetworkPlayer" prefab into the Player Prefab slot on the NetworkManager.
  1. 9. When you click "Start Host" in your UI, Unity will automatically spawn your networked player into the world!

12. Practice Exercises

  1. 1. What specific C# base class must your scripts inherit from to access Unity's Netcode functionality?
  1. 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 ServerRpc and a ClientRpc in Unity's Netcode for GameObjects. Walk through a scenario utilizing both to throw a grenade.
  • Q: Describe the concept of IsOwner in a multiplayer script. Why is checking if (!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, replacing MonoBehaviour with NetworkBehaviour. We protected our movement using IsOwner, synchronized transforms automatically, and fired custom network events using ServerRpc and ClientRpc.

17. Next Chapter Recommendation

Our game has complex physics, HD graphics, and multiplayer logic. It is probably running very slowly right now. Let's fix that. Proceed to Chapter 17: Game Optimization and Performance.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·