Posted on

Starting Multiplayer Games with Photon 2 in Unity

For this Unity Multiplayer Game Tutorial on how to make a multiplayer game in Unity using the Photon 2 plugin, we will be showing you how to create a Photon room controller script which will control some of the interactions between players connected to the same room. It will also control the start of the game and how the players are loaded into the multiplayer scene. There are two different ways to start a multiplayer game. One is a continuous load and the other is a delayed start. a continuous load means players can join and leave a game in progress at any time. whereas a delayed start is when all the players have to be connected to the room before the game can start. Our code in this video will handle both these methods and you will be able to switch between the two by changing a single bool.

We will start this lesson by creating two new C# scripts the first will be called Multiplayer Setting and the next one will be called Photon Room. We will then open these C# scripts up.

We will start in the Multiplayer Settings. We will first need to create some new variables. We can then delete the start and update functions. we will then create an awake function and set up a singleton for this C# script. We will then save this script after which we will make a few changes to our room creation in the lobby script.

We then need to open our Photon Room script. Inside this script, we will create some more variables. We will then create some code that will either load the player right into the multiplayer scene as soon as they connect to the room or the game will have a delayed start where all the players will load into the multiplayer scene at the same time. Once we have finished creating this script we will then save and return to Unity.

In Unity, we need to create an empty game object and attach our Photon room script. We will also create an empty game object and attach our multiplayer settings script. We then need to set the variable of these scripts. We will then create a placeholder for our Photon player object.

If you have followed along with the video tutorial you should then be able to build your project and run your multiplayer game, connect at least two instances of your game. This should then load your players into the multiplayer scene and you should be able to see the two players.