2 0 2 3

Getting started building multiplayer games with Unity

Arturo Nereu

Developer Relations

Unity Technologies

x.com/ArturoNereu

A story

Conker's Bad Fur Day - Rare Limited

What's your story?

The opportunity

77%

Steam

Apptopia

->

->

->

Of players play multiplayer games. [1]

Top sellers, usually multiplayer games. [2]

Most downloaded and revenue drivers are multiplayer games.[3]

[1] Unity Multiplayer Report 2022. [2] Us Top Sellers retrieved 08/16/2023. [3] Worldwide Top Free on iOS + Android, by Revenue retrieved 08/16/2023.

What is a Multiplayer game?

Definitions

Multiplayer Games

Asynchronous

Synchronous

->

->

Synchronous Multiplayer Games

My PC - UK

Remote PC - Canada

Synchronous Multiplayer Games

My PC - Denmark

Other Player's PC - USA

Single-player to Multiplayer

  • How do my players discover each other?
  • How do the players/clients send data to each other?
  • What happens if a player disconnects halfway through the match?
  • How do I deal with slow internet?
  • How do I support players in different regions of the world?
  • How do I save the rankings?
  • How do I make sure matches are balanced?
  • How do I update the game?
  • My game is successful; I have thousands of players. How do I scale?
  • How do I allow my players to communicate with each other?
  • How do I prevent cheating?
  • How do I support consoles now?
  • How do I allow my players to play cross-platform?

Multiplayer 'buckets'

Networking

  • Transport
  • Netcode

Service Infrastructure

  • Hosting
  • Matchmaking
  • Authentication
  • ...

->

->

What does Unity offer?

Networking

  • Transport
  • Netcode for GameObjects
  • Netcode for Entities

Hosting and Services

  • Game Server Hosting (Multiplay)
  • Matchmaker
  • Voice and Text Chat (Vivox)
  • ...

->

->

Architecting your multiplayer game.

  • Game
    • MetaGame
  • Hosting Models
    • Server hosted
    • Client Hosted
    • Peer to Peer
  • Authority
    • Player
    • Server

Moon, Aaron. Saintonge, Kiki. - How to build & design your multiplayer game based on genre. (2023)

Demo

  • Four-player Co-Op.
  • 1-minute gameplay session.
  • Session-based.
  • Desktop.

Galactic Kittens

  • Unity 2021.3+
  • Windows, macOS, Linux, Android, iOS, and consoles*.
  • Better suited for small-scale co-op.

Netcode for GameObjects

using System;
using UnityEngine;
using Unity.Netcode;

public class PlayerShipMovement : NetworkBehaviour
{
// ...

    void Update()
    {
        if (!IsOwner)
            return;

        HandleKeyboardInput();

        MovePlayerShip();
    }

// ...
}

Megacity

  • 64+ players.
  • Netcode for Entities.
  • Unity Gaming Services.
  • Free on GitHub.

Demo

  • Two-player competitive.
  • 10-second gameplay session.
  • Asynchronous gameplay.
  • Mobile / Web.

Magic 3

  • Cloud Code*
  • Cloud Save
  • Lobby
  • Vivox
  • Economy
  • Remote Config

Unity Gaming Services

// Entry point requested by the game client
module.exports = async ({ params, context, logger }) => 
{
	// Parse the received parameters
    let playerMove = params.playerMove;
    
	// Get opponent data (from Cloud Save)
    let opponentMove = await cloudSaveAPI.getItems(...);
    
    // Run the combat logic 
    const gameResult = computeResult(playerMove, opponentMove); 
    
    // Return the response with the result
    
    return 
    {
    	opponentMove: opponentMopve,
    	playerMove: params.playerMove,
    	playStatus: gameResult
  	};
}

function computeResult(playerMove, opponentMove)
{
	// combat logic
    
    return gameResult;
}

CombatLogic.js

// ...
using Unity.Services.Authentication;
using Unity.Services.CloudCode;

public struct CloudCodeResponse
{
    public string opponentMove;
    public string playerMove;
    public string playStatus;
}

public class CloudCodeManager : MonoBehaviour
{
    public async void Awake()
    {
        await UnityServices.InitializeAsync();
        await AuthenticationService.Instance.SignInAnonymouslyAsync();
    }

    public async void PlayMonster(int monsterType)
    {
        var arguments = new Dictionary<string, object> { { "playerMove", monsterType } };
        var response = await CloudCodeService.Instance.CallEndpointAsync<CloudCodeResponse>
        ("CombatLogic", arguments);
        
        // Show the player the results
        _gameManager.InstantiateResults(response);
    }
}

CloudCodeManager.cs

Thank you!

Arturo Nereu

x.com/ArturoNereu