Posted on

Observable Components with Photon 2 in Unity

For this lesson, we will teach you how to stream data across the network by making your own observable components. The Photon plugin already provides some observable components such as the Photon Transform View and the Photon Animator View. Although these premade observable components help with much of the networking in a multiplayer game there might be some additional values that you need to sync across the network.

Peer Play Tutorial

To create an observable component you need a script. You will then need to include using Photon.Pun namespace at the top. You will then need to have your script inherit from MonoBehaviourPunCallbacks and IPunObservable. You will then need a variable that you want to have synced. Once you have all of this you then need to a special callback function Which is called OnPhotonSerializeView. Inside this function, you will have an if statement for sending variable and then an else statement for receiving the variables.

Back in Unity, you need to add this new script to the observable components of the Photon View component of the object this script is attached to.

Posted on

RPC Functions with Photon 2 in Unity

For this lesson on how to make a multiplayer video game in Unity with the Photon 2 plugin in Unity 3D, I will show you how to create a character selection option that will then synchronize the selected character across the network. Features like the are prevalent in many video games today such as Rainbow Six Siege, Call of Duty Black Ops 4, and Fortnite. This lesson will teach you the basics of RPC or Remote Procedure Call Functions. RPC’s are probably one of the most important principles in developing a multiplayer video game. It is through RPC functions that we are able to sync data from one client to another. After following this tutorial you should be able to create your own RPC function and implement them into your own video games.

In between lesson I create a menu system with four different buttons. This will allow us to selected different characters for us to use as your player avatar prefab. We will begin by creating a new C sharp script called Player Info. This script will save the value of the character our player has chosen.

We then need to create a new C# script that will control do a new menu system. Inside this function, we only need to have a public function that will set the value of the character model we want to use.

We then need to create a Player Info object and a Menu Controller Object. We will then set the on click events of your buttons in our menu system.

We then need to create another C# script. Inside this script, we will create an RPC function that will synchronize our character model value across the network and instantiate it.

If you followed along with the video you should now be able to build your project. Once built you should be able to select different character models which will then be instantiated into the multiplayer scene once connected.

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.