Unity + Photon troubleshooting
Camilo Diaz
INTRODUCTION
This is a compilation of my experiences developing a collaborative VR application on Unity and Photon plugin. I go through a series of steps on how to work in a multiplayer environment and how to solve some errors.
FIRST OF ALL - WHAT IS PHOTON ENGINE?
Some of us have the misconception that photon is similar a version of Unity, but for multiplayer games. It's not. It is a service in the cloud that host your application-server side logic so you don't have to deal with networking heavy lift issues such as synchronization, timeouts, packet management, etc.
Unity is a game engine responsible for rendering, physics, update animations and manage user interaction, in other words the client side logic. Every time you update a game object position, activate a button or destroy any element in the scene you need to send that information over the net to the server, and the server synchronizes the information across all the clients. This is what Photon does.
THE BEST OF ALL IT'S FREE AND YOU CAN USE AS MANY APPS AS YOU WANT IN MULTIPLAYER MODE. THE ONLY LIMITATION IS THE NUMBER OF PLAYERS CONNECTED TO THE SERVER.
You don't need to deal with server side configurations ( well, this is not entirely truth, but the settings you need to change are minimum), you just send the information to the server. This will save you a lot of time.
How to install Photon Plugin in Unity
There are multiple tutorials on how to do this, most of them miss a few steps or are outdated (from 2016).
Go to the Official Photon Engine website and create an account, and lastly sign in.
Once you are in you account land page, dongt worry about the general settings. Just go to "reate a new APP"
Select the "Multi player Game" option. I also tried the "Non Gaming App" , and I didnt feel any difference. But I am not sure if the PUN plugin in Unity sees any difference between those two. Just go with the first option
Once your application is create it, it will give you an APP ID. keep this number in mind, you will need it later.
In order to install the Photon plugin in Unity YOU NEED A UNITY HUB ACCOUNT. GO to this website and create your account.
QUESTION: In the Unity package manager/ marketplace there are multiple photon plugins, what is the right one?
ANSWER: Select the one that says " PHOTON PUN 2 +"
As soon as you install the plugin in your Unity project, Unity will pop a window asking for a Photon APP ID. Go to the Photon Engine website landing page and get the APP ID of any of the applications you have created.
QUESTION: Can I use the same APP ID in multiple Unity projects?
ANSWER: YES. you don't need a new photon app every time you create a Unity project. Just use the same ID.
ISSUE:
After installing Photon plugin, Unity did not pop any window up asking for APP ID
Workaround:
Unfortunately, There is no easy fix for this. Something got burned in your Unity configuration files and it picks the Photon APP ID as a default in every project. This is the worst case scenario. Basically, I had to install a new Unity version, create a new project and move all the elements from the previous Unity install, and reinstall Photon Plugin. There are posts from back 2019 reporting this issue and it has not been fixed in 2023.
VIDEO TUTORIALS
The best video series on how to write a VR multiplayer application in Unity is this one:
Part 1 (Installation and setup and spawning networking game objects)
https://www.youtube.com/watch?v=KHWuTBmT1oI&t=387s&ab_channel=Valem
Part 2 (stream positions and game object ownership)
https://www.youtube.com/watch?v=DB5bajOMdUQ&t=195s&ab_channel=Valem
Part 3 (create multiple rooms )
https://www.youtube.com/watch?v=9Zqcv9k9cg0&t=903s&ab_channel=Valem
Make sure to watch all the videos before jumping to implementation. The creator corrects himself A LOT from one video to the other. So you might find yourself in issues that he solves at the end of the video or in part 3.
Also, he uses Unity 2019 and the latest LTR is 2021. Some things have changed since then:
The XR tool it main class is called XROrigin.
Ignore the last part of Part 1. His implementation only support local transformations and you need world transformations.
THIS IS VERY IMPORTANT
Keep the Photon View transform component in the Root game object. If you need to update local transformations ( specific parts of the body ) then add another Photon View transform to those part as well. If you dont do this, the other clients wont be able to see the other players move in multiplayer mode.
In the video tutorials, he tries to clean/organize his component dependency as much as he could, moving and removing photon components from multiple entities. I also did it, but it broke my simulation a day after the presentation and I couldnt solve the issue on time.
Just add the Photon View transform to every game object you need to synchronize online.
ALSO VERY IMPORTANT
Make sure all the prefabs transformations are set to their origin values. 0,0,0 for position and rotation and 1,1,1 for scaling. To do this, go to the Tranformation component and click on the triple dot right next to the name, select "reset".
Question : What happens if you don't set transformation to its default values?
Answer: Spawning networking objects will be set on random position, rotations and scales.
How to test multiplayer
Unfortunately, there is no easy way to test it. In the VR application case you need whether multiple HMDs. But if you only have one HMD, you can run an instance of your application from your computer and run another instance in your HMD. However, because your hardware limitations, only one instance is able to display in the VR headset and have access to the controllers input.
Test locally using Unity (Windows)
First, Build you application for PC. Go to File -> Build settings -> select window platform and add the scenes. Select the folder where the application will be exported.
Run the application in Unity using the "Play" Button
Now, go to the folder where the built application is located. Open the applications and the HMD will switch to the unity ran instance to the built instance. The unity instance still runs but it loses all the input from the headset and the controllers.
If you check the Project hierarchy in Unity, you will see multiple players in the scene. This is also explained in the videos, but the creator misses an important point. If you select any of the networking players you can see their properties, such as transformations, physics and collision properties. That way you can debug their positions and user interactions in real time.