Posted on

RPC in FPS with Photon 2 in Unity

For this tutorial lesson, on how to make a multiplayer game in Unity using the Photon 2 plugin we will be using the RPC functions we have learned about to make a basic first-person shooter control that will sync across the network. This tutorial will teach you how to use the RPC function of the Photon 2 plugin in other scenarios. After this lesson, you should have a better understanding of RPC function and what they can do as well when to use them. You will also learn how to take any game machinic and make it work in a multiplayer game.

We will begin by creating variables in our Avatar setup script for the first one will be player health and the next one will be player damage. We will then create a new C# script called Avatar Combat. In this script, we will create some new variables. We will then initialize these variables. We will then create a shooting function and we will use Raycast to do this. We first want to check for player input and then we will create our Raycast. If the Raycast hit another player then we want to remove health from that player. We will then save our C# script and go back to Unity.

In Unity, we will create and add a new camera to our player avatar object. We then need to attach our new scripts to their respective objects and set the variables.

We will then go back to our Avatar combat script and we will turn this script into a script that will work across the network. We will do this by creating an RPC function that will synchronize the player’s health across the network when the player has been shot.

If you followed along with the video then you should now have a working basic FPS controller that networks the player’s health when shot.

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.