Posted on

Unity Multiplayer – Delay Start

For this lesson, I will teach you how to create a delay start matchmaking system using the Photon 2 plugin in Unity. In the last video, I showed you how to create a quick start matchmaking system which is where the players can click one button that will connect them to a photon room and load them into the multiplayer scene. This is mostly used in games where the players can join an ongoing game. A delay start system is where the players can click one button which will connect them to a photon room but instead of loading the multiplayer scene it will instead load a waiting room scene. Once the minimum players have joined a timer will start and once the timer runs out all the players in the room will be loaded into the multiplayer scene at the exact same time. This is a better system for games that need to be played from beginning to ends such as Rainbow Six Siege, PUBG, and Fortnite.

Menu Scene in Unity

To get started we will first create the menu scene in Unity. This scene needs to have three buttons. one for showing before we have connected to the Photon servers, one for starting the search for a room, and the last for canceling the search. Once you have created the menu scene we will then code two scripts for this scene. One will be the lobby controller and the other will be the room controller.

Coding the Delay Start Matchmaking in C#

In the lobby controller script, we need to automatically sync the scene loading. we will need to create a function to pair to the start button. This function will first try to join an existing room. If it fails to join we will then try to create our own room. If we fail to create a room we will then just keep trying to create a room. We will also need to create a function for our cancel button. In our room controller script, we will need to load the waiting room scene when the player successfully joins a photon room.

After this save your scripts and go back to Unity. Inside Unity and the menu scene, you will need to add these script to the scene. You will then need to set the variables of these scripts and pair the public functions to the different buttons in the scene.

Waiting Room Scene in Unity

The next thing we need to do is create the waiting room scene. This scene needs to have a cancel button, a player count text display, and a timer text display. Once you have created this scene we will then need to create one script for this scene which will be our waiting room controller.

Coding the Countdown and Waiting Room

In this script, we will first update the displays for the player count and the timer. We will then need to update the displays whenever a new player joins the room or when a player leaves the room. We will then need to count down the timer in the update function if there are enough players in the scene. if the timer gets down to zero then we will load the master client into the multiplayer scene which because of automatically syncing the scenes will in turn load all the other players into the same scene.

Once you have all this you can then save all your scripts and go back to Unity. Inside Unity, you need to add this script to your waiting room scene. You will then want to set the variables in the inspector and pair the function to the cancel button.

After this, the last thing you need to have is your multiplayer scene which we created in our quick start tutorial. If you followed along with this tutorial you should now have a working delay start matchmaking system for your multiplayer games.

Posted on

Unity Multiplayer – Quick Start

This is the second lesson in this Unity multiplayer tutorial series. In this lesson, we will teach you how to make a quick start matchmaking system. This will allow the player to click one single button which will then load them into a multiplayer match.

Unity UI setup for Quick Start

In this tutorial, we will begin by creating the UI for our quick start matchmaking. For this UI in Unity, we will need three UI buttons. One of the buttons we will disable the interactable option of the button, this will be for before we connect to the Photon servers. The second button will be our join or quick-start button and the third button will be a cancel button. We will need to hide these last two buttons in the hierarchy. Once we create these button we will need to create a new script which will be for this quick start mechanic.

Coding Quick Start Matchmaking in C#

In this new C# script, we will need to first create a callback function for when our play first connects to the Photon servers. in this callback function, we want to enable the quick-start button. We will then need to create a public function that we can pair to the On Click of the quick-start button. In this function, we need to make it so that the players first try to join an existing game but if no game is available then we will try to create a new game. When we successfully join or create a new room we will then load the players into the game scene and instantiate a player object in the scene for each player. We then need to create another public function, that will be for the cancel button.

Working in Unity

Once you have this script created, we will then go back to Unity. In Unity, we will need to create an empty game object to attach this script to. We will then need to set the value of our variables and you will need to set the on click functions of the various buttons. Next, you will need to create a player prefab object. Finally, we will need to create a new scene to be our multiplayer game scene.

Testing your Project

If you followed along with this tutorial you should then have a working quick start matchmaking system for your game. When you test your project you will know the quick start is working when you see two instances of your game load into the multiplayer scene and in the hierarchy, you can see two different player prefabs with each being owned by a different user.

Unlock Code and Member Content

Posted on

Unity Multiplayer Tutorial – Setup

Photon 2 Tutorial

Welcome to this new tutorial series on creating multiplayer video games in Unity using the Photon 2 PUN plugin. For this lesson, we will show you how to download, install and set up the Photon 2 plugin in your Unity projects. Before you follow along with any of the other lessons in this tutorial series, you must complete this video. We will be providing the downloadable add-ons for all the other videos in this series but these add-ons will not include the original Photon plugin. You must get that from the official Photon website.

Photon Website: https://www.photonengine.com/
PUN 2 Plugin:
https://assetstore.unity.com/packages/tools/network/pun-2-free-119922
Payment Plans: https://www.photonengine.com/en/pun/pricing
Documentation: https://doc.photonengine.com/en-us/pun/current/getting-started/pun-intro
Scripting API: https://doc-api.photonengine.com/en/pun/v2/index.html

Creating a Photon Account

The first thing you will need to do in this lesson is to create a Photon account. Once you have registered an account you will need to create a new Photon app project. You will then need to fill out some information about your multiplayer game and then you will click create. After creating a new app you will need to copy the AppID for later.

Setting up a Network Connection

Back in Unity, you will need to go to the Unity Asset Store and search for the PUN 2 free plugin. You will need to download and install this plugin in your project. After importing this plugin a new window will appear. This is the PUN wizard and you need to paste your project AppID into this window and then you will click setup project. The next thing we will do is establish our very first connection to the Photon master server. To do this we will need to create a new script. In this new script, the only thing we will do for this tutorial is to initialize the network connection.