Posted on

Photon Transform View with Photon 2 in Unity

Here is the next lesson on how to make a multiplayer game in Unity using the Photon 2 plugin. For this lesson, we will be focusing on player movement and synchronizing the transform across the network using the photon transform view. It is very important to be able to sync player movement across the network because without doing so players will be able to see each other in different locations then they actually are. It is also important to make sure that each player can only control his own avatars.

The first thing that we need to do is open up our Photon Player Avatar prefab. On this prefab, we will add the Photon Transform View so that our player avatar can have its movement synchronized. We will then need to create a new C# script for setting up the controls for the movement of the Avatar object. With this C# script opened the first thing we will do is create some new variables. We will then initialize all our variables. As we create the movement functions we first need to check for the player’s input. Based on the player’s input we will then move the player avatar object. When we call these movement functions is the update function we will use the Photon View variable to check to see if this object is owned by the local player. This is to make sure that only the local player can control their avatar and not other people’s avatars. We will then save our script and return to Unity 3D.

If you follow along with the video you should now be able to build your project. In the multiplayer scene, you should be able to now control the movement of only your local player avatar.

Posted on

Instantiating Player Avatars with Photon 2 in Unity

For this Unity Multiplayer Game Tutorial on how to make a multiplayer game in Unity with the Photon 2 Plugin, we will teach you how to spawn a player avatar or character. This will be an object that the players can control and use to play the game. This Avatar could be a person in a first-person shooter or a hero dungeon crawler RPG like Diablo 3. It is very important that you keep the Avatar as a sperate object the first photon network player object that is already spawned into the multiplayer scene.

We will begin by looking at how our game currently works. Our game currently has the Photon Player object that is the only object in our scene that is associated with our players because of this we need to make sure that we maintain this player object through the duration of our multiplayer game.

We will first create a new C# script in Unity we will call this C# script Photon Player. In this script, we need to include the Photon.PUN namespace we will then create some variable is this script including a Photon View variable. This script will be in charge of instantiating our player’s avatar object into the multiplayer scene.

In Unity, we need to create some empty game objects to be our spawn points transforms. We then need to create a player avatar prefab. We will need to set the variables of our scripts.

If you followed along with the video, you should have a working multiplayer game that allows you to instantiate new player avatar objects into the scene.

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.