There are a few game objects that have to be synchronized in our game, including the goals, the puck, and the mallets. We also need to make a few adjustments to the scene and set up matchmaking.
We are going to use the Photon Playmaker example as a template for our game, using the scenes from there and modifying them to suit our purposes. This is generally a good idea if you want to set up multiplayer quickly, because these examples feature numerous and complex FSMs that would take quite a lot of time to set up, while most of the things you will need have already been implemented.
You can download the demo scenes here: http://www.hutonggames.com/samples.php. Simply click on the link Download PlayMaker Photon Demo (requires Unity 3.5+ Playmaker 1.6.1+). Then you will need to find the downloaded unitypackage
file on your computer and double-click on it. This will prompt the import window. Click on the Import button in the bottom-right corner of it.
The warning on the example download page advises against importing the example into existing projects. We can ignore this warning, because none of our files are named in the same way the ones in the example are. As a general rule, you should always make sure that this is the case before importing new packages into new empty projects. Otherwise you might find yourself losing important assets in your projects.
Save the Multiplayer scene, and let us add the demo scenes to our project. They should be under Photon Unity Networking/PlayMaker/Demo/Separated Scenes Demo
. Open the demo_lobby scene by double-clicking on its file.
The first thing we need to do is add the new scenes to our project. Open Build Settings by pressing Shift + command + B (Shift + Ctrl + B in Windows), then click on the Add Current button shown in the following screenshot. This will add the currently open scene to the project. Do this for demo_lobby and demo_room. Then close Build Settings.
These two scenes are going to be responsible for two states of the game: the matchmaking state where players can find and create servers and the match state where the game itself takes place. You can test how it works by opening two instances of your game at once: one in the Unity Editor, one in your web browser.
Build
folder to make sure that you can always find your build. This should automatically open the game in your browser.Please note that your platform has to be set to Web Player as described in Chapter 1, Getting Started with Unity and Playmaker. If it is not, open Build Settings, select Web Player from the list on the left, and click on the Switch Platform button. Then close the Build Settings window.
Now that you have seen how multiplayer works in the example project, it is time to modify its scenes to work with our game. We are going to keep the matchmaking intact (in fact, it can be kept this way for almost any multiplayer game). The only thing that needs changing is the maximum number of players that can join a single room. You may have noticed that the example project allows for 100 players. We need to change it to 2
.
2
. Now up to two players will be able to join one room.Now that the lobby is all set, it is time to set up the game itself to work via the network.
Goal
, Mallet
, and Puck
under Prefabs/Resources. Create the folders if you don't have them yet.Goal
prefab, MalletLeft game object into the Mallet
prefab, and Puck game object into the Puck
prefab.Blocker
, since they are needed to block the mallets.Mallet
prefab into the GameObject slot of player prefab and the Puck
into the the GameObject slot of puck prefab.Two Players
.0
, 0
, 0
).Goal
prefab from the Project panel. Set the Position property to (-7.98
, 2
, 0
) and the Rotation property to (0
, 90
, 0
). Set Store Object to goalRef.How many players?
and add Photon Network Get Room Properties and the Int Compare actions to it. Make sure the former is before the latter in the list. If an error saying that PlayMakerPhotonGameObjectProxy component is required appears, click on it, and the component in question will be added automatically.2
. Set Equal to Two Players. Check the Every Frame box. If you see an error about an event, ignore it for now; it will be fixed later on.Create Puck
.Puck
prefab variable. Set Position to (0
, 0.3
, 0
) and Rotation to (0
, 0
, 0
).7.98
, and Y and Z are None.This state machine is responsible for spawning objects that should be unique to different players: players' mallets and goals are spawned as soon as the players connect to the room, while the puck is spawned once the second player connects to make sure that player 1 does not win while alone in the room.
Now we are going to set the individual parameters and synchronization of each of the prefabs we created, starting with Goal
.
Goal
prefab in the Project panel and add three components to it using the Inspector panel: PlayMaker FSM (Script), Photon View, and Play Maker Photon Game Object Proxy. You can find these components by clicking on the Add Component button in Inspector and typing their names in the search field.Goal
prefab into the Photon View component's Observe property.ColorSync
using the FSM tab of the playMaker panel.is mine?
and add two events to it: YES and NO.This state machine makes sure that goals change color in the beginning of the game. Verify that you still have the PlayMaker Photon Proxy game object in the scene. If you don't copy it from the Multiplayer scene. At this point you may want to build and launch two instances of the game to test that everything works well.
Now let us change the color of the mallets to match the goals' colors. We will also make sure that only the owner of the Mallet can move it.
Fsm Photon player
prefab under Photon Unity Networking/PlayMaker/Demo/Resources
in the Project panel and select it.Mallet
prefab by right-clicking the components' headers of Fsm Photon player
and selecting Copy Component from the drop-down menu and then right-clicking one of the components' headers in the Mallet
prefab and selecting Paste Component As New from the drop-down menu.Fsm Photon player
prefab's Play Maker FSM components: the ones called GameObject naming, Position synch, and variable synch repository.Movement
to make sure we remember what it does.Bool
variable to it called isMine.0.85
, and Z to None.Now the mallets should be good to go, their positions synched over the network and colors set on startup. Save your scene and confirm that mallets' positions are synced by building the game and launching two instances of it while connecting to the same match.
All that is left is to sync the Puck's position over the network.
Puck
prefab and add the Photon View and Play Maker Photon Game Object Proxy components to it.Fsm Photon player
prefab.0.3
, and Z to None.The last thing that you need to do is make sure that one of the players leaves the room instead of just reloading the level once the puck hits a goal. To do that, select the GoalTriggerLeft game object and, in the LoadLevel state of its FSM, replace the Load Level action with Photon Network Leave Room. Do the same for GoalTriggerRight.
Now your multiplayer should be set up. Before testing everything, confirm that you have copied all of the files over from the Multiplayer scene correctly. Make sure that there is a game object called PlayMakerPhotonProxy in both demo_lobby and demo_room scenes. If it is missing in one of the scenes, add it by going to PlayMaker | Addons | Photon Networking | Components | Add Photon proxy to scene from the main menu, then save the scene.
You can test it by making a build and opening it in your browser while launching the game in the Editor. Make sure you always start from the demo_lobby scene, because otherwise the matchmaking will not work.
You will see that the mallets' and the puck's positions are synched over the network. That is, they move simultaneously in both Editor and web browser. You will also notice that the movement of the remote object is somewhat jerky and imprecise (especially the puck on the host's client). This is due to the inevitable network delay and physics data being calculated more often than Photon packages are being sent. At the moment the only real solution to this problem is switching to Unity native networking, which, however, requires that you have your own server if you want the game to work over the Internet.
The place where the objects' movement is smoothed out is the Position synch FSM of Puck
and Mallet
prefabs. If you look at the get player position and Set player position with lerp states of these state machines, you will see that, if the object was first created on the local machine, its position gets saved in a variable every frame and synched over the network. If the object does not belong to the player's client, the variable is read, and then the position of the local copy of the object is interpolated.
You could try changing the Amount property of the Vector3 Lerp 2 action in the Set player position with lerp state. This variable determines the precision of the interpolation. If you want to know more about the way PUN works under the hood, make sure you consult its online documentation that explains every state machine in the demo scenes in great detail: https://hutonggames.fogbugz.com/default.asp?W927
As you can see, Photon networking is quite easy to set up, but is not a very good solution for games that require physics simulation. This is mostly due to cloud server limitations and limited integration with the Unity engine. Most simple games that don't rely on physics so heavily can benefit from Photon greatly. In other cases, Unity native networking should be used. More information about it can be found in the Network Reference Guide section of the Unity documentation: http://docs.unity3d.com/Documentation/Components/NetworkReferenceGuide.html
Playmaker is capable of working with native Unity networking as well. You can check the Network section of the Actions panel for the list of available Playmaker actions and compare them with the actions described in the reference material.
18.225.57.67