Play Together!
Multiplayer Basics
The finale: real networked multiplayer with Unityโs official Netcode, host and join, synced player movement, server authority, RPCs for effects, and a shared score. Two windows, one game.
The finale: real networked multiplayer with Unityโs official Netcode, host and join, synced player movement, server authority, RPCs for effects, and a shared score. Two windows, one game.
Install Netcode for GameObjects and grasp host/client/authority.
Make a player prefab that spawns and syncs for every joiner.
using Unity.Netcode;
using UnityEngine;
public class NetPlayerMove : NetworkBehaviour
{
public float speed = 5f;
void Update()
{
if (!IsOwner) return; // only drive YOUR player!
Vector2 input = new Vector2(
Input.GetAxisRaw("Horizontal"),
Input.GetAxisRaw("Vertical")).normalized;
transform.position += (Vector3)(input * speed * Time.deltaTime);
}
}
void Update()
{
if (!) ;
// ... movement ...
}
Actually run multiplayer: a build as host, the editor as client.
Share game state correctly with NetworkVariable.
using Unity.Netcode;
using TMPro;
public class ScoreBoard : NetworkBehaviour
{
public NetworkVariable<int> score = new NetworkVariable<int>(0);
public TMP_Text label;
public override void OnNetworkSpawn()
{
score.OnValueChanged += (oldVal, newVal) =>
label.text = "Score: " + newVal;
}
public void AddPoint()
{
if (!IsServer) return; // authority!
score.Value += 1;
}
}
Use RPCs for actions and effects across the network.
using Unity.Netcode;
using UnityEngine;
public class PlayerEmote : NetworkBehaviour
{
void Update()
{
if (IsOwner && Input.GetKeyDown(KeyCode.E))
EmoteServerRpc();
}
[ServerRpc]
void EmoteServerRpc()
{
EmoteClientRpc(); // server approves & broadcasts
}
[ClientRpc]
void EmoteClientRpc()
{
// runs on EVERY client:
Instantiate(confettiPrefab, transform.position,
Quaternion.identity);
}
}
]
void EmoteServerRpc()
{
();
}
[ ]
void EmoteClientRpc() { /* effect */ }
Assemble a 2-player coin-race and close out the series.
Keep the Unity cheatsheet handy
MonoBehaviour, physics and UI in one place - use it beside this workshop.
Design your multiplayer game art!
Use Pixel Studio to create player avatars and UI icons - give your Unity multiplayer game a polished look.
Vector2 movement = new Vector2(moveX, 0);void Update() {}float moveX = Input.GetAxis("Horizontal");rb.velocity = movement * speed;