# Nakama and Photon Fusion demo

**URL:** https://heroiclabs.com/docs/sample-projects/games/nakama-photon/
**Summary:** A multiplayer platformer built with **Unity 6** and **Photon Fusion**, with player accounts, friends, and leaderboards powered by **Nakama**.
**Keywords:** nakama photon, photon fusion, custom authentication, nakama friends, nakama status, nakama leaderboards, unity
**Categories:** sample-projects, nakama-photon, games

---

A multiplayer platformer built with **Unity 6** and **Photon Fusion**, with player accounts, friends, and leaderboards powered by **Nakama**.

- Download: https://github.com/heroiclabs/nakama-photon-demo

![Nakama and Photon Fusion demo](https://heroiclabs.com/docs/sample-projects/games/nakama-photon/images/nakama-photon-hero_hu5050444067362802125.webp)


The _Nakama and Photon Fusion demo_ is a multiplayer platformer that combines two tools in one Unity game: [Photon Fusion](https://doc.photonengine.com/fusion/v2/fusion-intro), a networking library that keeps players in sync during a match, and [Nakama](/docs/nakama/), an open-source game backend that stores player accounts, friends, and leaderboards. The game is built on Photon's [Fusion Starter](https://doc.photonengine.com/fusion/v2/game-samples/fusion-starter/overview) sample, with Nakama added on top.

| Specs               | Description |
| ------------------- | ----------- |
| **Engine**          | Unity 6 with [Photon Fusion 2](https://doc.photonengine.com/fusion/v2/fusion-intro) in shared mode |
| **Server**          | Nakama 3.42 ([Go runtime module](/docs/nakama/server-framework/go-runtime/)) |
| **Nakama features** | [Authentication](/docs/nakama/concepts/authentication/), [Friends](/docs/nakama/concepts/friends/), [Status](/docs/nakama/concepts/status/), [Leaderboards](/docs/nakama/concepts/leaderboards/), [Hooks](/docs/nakama/server-framework/introduction/hooks/) |

> **Looking for an in-depth walkthrough?:** For a step-by-step look at the code behind each feature, see [How to add Nakama to a Photon Fusion game](/docs/nakama/guides/concepts/photon-integration/).

## Features

- **Real-time multiplayer**: Up to eight players race through the same level in a Fusion session, collecting 10 coins, crossing falling platforms, and reaching the flag first. In shared mode, the session runs in Photon Cloud without a game server, and each player's client controls their own character.
- **Authentication**: Players sign in to Nakama when the game starts. Photon Cloud checks each player's Nakama session token before it lets them join a match.
- **Friends**: Players add each other by username, see whether a friend is in the menu or in a game, and join a friend's match from the friends list.
- **Leaderboards**: The winner of each round saves their time to a leaderboard. The server rejects times that are too fast to be real.

For the full list of gameplay and networking features that come from the Fusion Starter, see Photon's [Fusion Starter overview](https://doc.photonengine.com/fusion/v2/game-samples/fusion-starter/overview).

## Why use Photon and Nakama

Photon Fusion is built for the match itself. It keeps game state in sync between players while a match is running, but it doesn't keep anything once the session ends. Nakama is built for everything around the match: player accounts, social features, and records that last from one session to the next.

Together, they cover both halves of a multiplayer game. Fusion handles the fast, moment-to-moment gameplay, and Nakama gives players an account, friends to play with, and leaderboards to compete on, all of which carry over from one match to the next.

## Try the demo

To play against yourself, open this page in two separate browser sessions. You can also send this page to a friend and play together.

  <iframe
    title="Nakama and Photon Fusion demo game"
    src="https://itch.io/embed-upload/20641180?color=333333"
    style="position: absolute; top: 0; left: 0; width: 1280px; height: 780px; border: none; transform-origin: top left; transform: scale(calc(100cqw / 1280px));">
  </iframe>

### Controls

**Keyboard and mouse**

- Move: **W**, **A**, **S**, **D**, or the arrow keys
- Look around: mouse
- Jump: **Space**
- Sprint: **Left Shift**
- Open or close the game menu: **Esc** or **Enter**

During a match, the mouse controls the camera and the cursor is hidden. To select the **Friends** or leaderboard buttons in the top-left corner, open the game menu:

> **Opening the menu in your browser:** Press **Esc** once to release the cursor, then press **Esc** again to open the game menu.

### Play with two players

In the steps below, Player 1 is your first window and Player 2 is your second window, or your friend.

1. As Player 1, open **Friends**. The game assigns you a random username, such as `SwiftOtter123`.
2. As Player 2, open **Friends**, enter Player 1's username in **Username to add**, and select **Send Invite**.
3. As Player 1, accept the invite. You're now on each other's friends lists.
4. As Player 1, select **Start Game**, and wait for the match to start.
5. As Player 2, check the friends list. Player 1 shows as **In Game**, with a **Join** button. Select **Join** to enter the same match.
6. Race to collect 10 coins and reach the flag first.
7. When the round ends, press **Esc** and select the leaderboard button in the top-left corner. The winner's time is in the list.

## Project structure

The repository has two parts:

| Path               | Purpose |
| ------------------ | ------- |
| `UnityFusionDemo/` | The Unity project, built on the Fusion Starter |
| `nakama-server/`   | The Nakama Go module, and a Docker Compose file that runs it |

In the Unity project, the Nakama code sits beside the Fusion Starter's own code:

| Path                                    | Purpose |
| --------------------------------------- | ------- |
| `Assets/02_Platformer/`                 | The platformer scene, prefabs, and gameplay scripts from the Fusion Starter |
| `Assets/02_Platformer/Nakama/Scripts/`  | Nakama sign-in, the friends list, and the leaderboard panel |
| `Assets/Common/Scripts/`                | Scripts shared across the Fusion Starter, including the game menu |
| `Assets/Photon/`                        | The Photon Fusion SDK |

The integration code is in these scripts:

| File                     | Purpose |
| ------------------------ | ------- |
| `NakamaSingleton.cs`     | Creates the Nakama client, signs the player in, and opens a socket |
| `UIGameMenu.cs`          | Starts the Fusion session with the Nakama session token, and updates the player's status |
| `FriendsList.cs`         | Loads friends, follows their status, and joins their match |
| `FriendEntry.cs`         | Shows a friend's status and the **Join** button |
| `Player.cs`              | Uses the Nakama username as the Fusion nickname |
| `GameManager.cs`         | Decides the winner in Fusion and saves the time to Nakama |
| `LeaderboardPanel.cs`    | Lists leaderboard records |

On the server, the Go module is split by feature:

| File             | Purpose |
| ---------------- | ------- |
| `main.go`        | `InitModule`, which registers every RPC and hook, and the hook that notifies a player when a friend removes them |
| `auth.go`        | The `photon_auth` RPC that Photon Cloud calls to check session tokens |
| `usernames.go`   | Gives each new account a random username |
| `leaderboard.go` | Creates the leaderboard, and the hook that rejects implausible times |

[Download the demo](https://github.com/heroiclabs/nakama-photon-demo)

## How it works

### Authentication

The game signs in to Nakama with [device authentication](/docs/nakama/concepts/authentication/#device) when it starts, and the server gives each new account a random username. When the player starts or joins a match, the client passes its Nakama session token to Fusion. Photon Cloud sends the token to the `photon_auth` RPC on the Nakama server, which validates it and tells Photon whether to accept the player. The player's Nakama username also becomes their nickname in the match.

For the code, see [Authenticate players with Nakama](/docs/nakama/guides/concepts/photon-integration/#authenticate-players-with-nakama).

### Friends and joining matches

The friends list uses Nakama's [friends](/docs/nakama/concepts/friends/) features to send, accept, and remove friend requests. 

Each player also publishes a Nakama [status](/docs/nakama/concepts/status/): `In Menu`, or `In Game:` followed by the Fusion session name. The friends list follows each friend's status and shows a **Join** button when a friend is in a match. Selecting **Join** starts Fusion with that session name, which puts the player in the same match.

For the code, see [Join a friend's session](/docs/nakama/guides/concepts/photon-integration/#join-a-friends-session).

### Leaderboards

![The in-game leaderboard panel listing three players by rank, with their usernames and finishing times](https://heroiclabs.com/docs/sample-projects/games/nakama-photon/images/nakama-photon-leaderboard_hu16197203629485265838.webp)

Fusion decides who wins each round, and the winner's client saves the time to a [leaderboard](/docs/nakama/concepts/leaderboards/). The leaderboard keeps each player's best time. In this demo, a [before hook](/docs/nakama/server-framework/introduction/hooks/) on the Nakama server rejects times under three seconds to filter out obviously fake times.

> **Shared mode and cheating:** Shared mode has no game server, so a player with a modified client can still submit a fake time. See [When to use an authoritative leaderboard](/docs/nakama/guides/concepts/photon-integration/#when-to-use-an-authoritative-leaderboard).

For the code, see [Save the winner's time](/docs/nakama/guides/concepts/photon-integration/#save-the-winners-time).

## Run the demo yourself

To run the demo on your own computer, or connect it to your own Photon app and Nakama server, see [Run the demo](/docs/nakama/guides/concepts/photon-integration/#run-the-demo) in the integration guide.

## Credits

- Gameplay sample: [Photon Fusion Starter](https://doc.photonengine.com/fusion/v2/game-samples/fusion-starter/overview)
- 3D models, sounds, and font: [Kenney Starter Kit 3D Platformer](https://kenney.nl) (MIT)
- Icons: [Kenney Board Game Icons](https://kenney.nl) (CC0)

## Additional resources

- [How to add Nakama to a Photon Fusion game](/docs/nakama/guides/concepts/photon-integration/)
- [Nakama friends](/docs/nakama/concepts/friends/)
- [Nakama leaderboards](/docs/nakama/concepts/leaderboards/)
- [Nakama leaderboards sample project](/docs/sample-projects/unity/nakama-leaderboards/)
- [Photon Fusion documentation](https://doc.photonengine.com/fusion/v2/fusion-intro)
- [Community forum](https://forum.heroiclabs.com/)
