Please note: development of Photon TrueSync & Thunder is ceased and we will not publish any further updates. While existings apps are not affected, we recommend to migrate apps that are still in development.

TrueSync Tutorial Part 1

Tutorial Contents

This tutorial series explains how to use Photon TrueSync to build a simple multiplayer game in Unity from the ground up. Photon TrueSync is a deterministic lockstep system, so all game code must be executed in discrete steps (we call them ticks) that are run in the same order in all client computers, so the end results are always the same.

The tutorial is organized in four incremental parts, each one showing some of the key concepts of TrueSync. At the end of each part, you can run the game as is to make sure you're progressing:

  • Part 1: intro, download and install - this introduction, download and installation instructions, plus basic Photon Unity Network (PUN) code to connect game clients to the Photon Cloud and have them join a networked game (a Photon room);
  • Part 2: moving boxes - setting up a TrueSyncManager game object, creating a player prefab with TrueSync's deterministic physics, adding code to move the player around and synchronizing input across game clients;
  • Part 3: shooting projectiles - synchronized instantiation and destruction of game objects, keeping score of deaths and kills;
  • Part 4: rollbacks and state tracking - using rollbacks to hide latency, avoid game state drifts by tracking the state of custom variables and objects;

What To Expect From Part 1:

A working game client that connects to the Photon Cloud and joins another client into a Photon Room.

Downloading TrueSync

Please, use the Asset Store tab (Window/Asset Store from inside Unity) to download and import TrueSync. You can search "TrueSync" from there or click "open in Unity" from our official store entry.

When opened from Unity, you'll see the option to download and then import it to the current project. Please do this into a newly created project.

truesync import window
Importing TrueSync into a fresh project.

TrueSync includes the latest version of PUN (Photon Unity Networking). PUN is used both to connect the game clients into Photon Rooms, and to provide the communication system that TrueSync needs.

Setting up PUN

You need a Photon account to use TrueSync, so if you don't have one yet please proceed to Photon website to create one now. Within your account, create and copy a new App ID to be used for this tutorial.

Now, go to the project tab in Unity and look for "Photon Unity Networking/Resources" and click the "PhotonServerSettings" asset. You must type your App ID (the one you created with your photonengine.com account), and choose a hosting region (we suggest using best region, as in the Figure bellow).

pun settings
PUN settings asset.

Don't forget to check "Auto-Join Lobby", as this will make things easier in the sections to follow.

Now lets use some code to make PUN connect to the Photon Cloud when the game starts.

Connecting and Joining Rooms

Create a scene in Unity, and save it as "Menu" under an also newly create folder that you might call "Tutorial".

We'll use this scene for basic PUN connection and room creation. Now create an empty C# script, and save it as "TutorialMenu.cs" in the same folder as above.

project tab with scene and script
Project tab with scene and script.

Now, attach the "TutorialMenu.cs" script to any game object in the "Menu" scene. In the figure bellow, it is attached to the camera:

script attached to camera
Script attached to camera.

Open the "TutorialMenu.cs", make the script inherit from the "PunBehaviour" class and type the following code inside the provided "Start()" method, so it looks like the code bellow.

C#

using UnityEngine;
using System.Collections;

public class TutorialMenu : Photon.PunBehaviour {

    void Start () {
        PhotonNetwork.ConnectUsingSettings ("v1.0");
        PhotonNetwork.automaticallySyncScene = true;
    }

}

This code will connect to Photon using the settings you chose in the previous section. After connecting to the Photon Server and joining the lobby (remember, we set PUN to auto-join the lobby), PUN will execute a callback that we can use to create or join a game room:

C#

public override void OnJoinedLobby() {
    PhotonNetwork.JoinOrCreateRoom ("room1", null, null);
}

This code will make PUN join a random room, or create one if it doesn't exist yet. There are many different options for room creation/join that you might look at PUN's documentation, but this will be enough for our purpose in this tutorial.

Loading Scenes

When room is correctly created and joined, Photon will execute another callback, and we'll use this to coordinate the load of a scene simultaneously in all clients connected to the room.

We separated the menu scene from the game scene isolate where the deterministic lockstep session will happen, coordinated by TrueSync.

Create a new scene, and call it "Game". Save it under the same "Tutorial" folder as with the other files in this tutorial. Now go back to the "TutorialMenu.cs" script, and include the following code.

C#

void OnGUI() {
    GUI.Label (new Rect(10, 10, 100, 30), "players: " + PhotonNetwork.playerList.Length);

    if (PhotonNetwork.isMasterClient && GUI.Button (new Rect (10, 40, 100, 30), "start")) {
        PhotonNetwork.LoadLevel ("Tutorial/Game");
    }
}

This will give the player at the master-client (the client that created the room) the power to ask Photon to load the game scene simultaneously in all clients connected to the room. It also shows how many players are currently connected to the room.

Testing

Please, make sure both scenes (Tutorial/Menu and Tutorial/Game) are included into the build settings, and that Tutorial/Menu is the first one (the one Unity will load at start up).

build settings
Build settings.

Create a build of your choice and run two copies of it, or one copy and run the Menu scene from inside the Unity editor. You should see both clients connecting to Photon, and then loading the empty game scene after you press start in the master client.

Now let's move on to Part 2 to learn how to control a player character over the network.

Back to top