Tutorial: Steam VR in Unity with Photon Pun 2
Paul Molnar, March 2022
Why Build an Application in Unity for Steam VR?
Steam VR is a great tool to build games for because it is a universal hub for almost every headset out on the market. If you are concerned your game/application may have issues on different headsets then building for Steam VR is the choice for you. Steam VR keeps an updated package on the Unity Asset Store and will automatically update your Unity project as soon as you import it. It has lots of different models and prefabs inside of the package which allow you to immediately start customizing your project such as choosing for your character to have crazy alien hands. Another great reason to develop for Steam VR is that there is lots of info out there about how to get started as it is one of the largest hubs for virtual reality on the internet. To learn more about Steam VR and why you should use it visit the Steam VR website at: Steam VR About Page.
**NOTE**: This is an intermediate tutorial for people with experience building projects in Unity. If you have no experience with building Unity Projects I recommend following along with the YouTube videos linked at the bottom of this page as it will clarify some steps
Project Set Up:
Note that this tutorial is accurate as of 2022. For years after 2022, scripts may need to be updated and packages may have changed. First open up a new 3D project in Unity with the latest version. There are known problems with some of the packages involved in this tutorial in earlier versions so it is recommended to use versions 2020 and after. Next you will have to head into the Unity Asset Store and install the following packages:
Steam VR Plugin
Once the package is downloaded and imported Unity may ask you a series of questions about updating your game to allow for synchronization to the Steam VR package. I suggest accepting all of these changes as it will automatically set up your game for Steam VR. It may also ask prompt you to change your lighting from linear to gamma which will affect the coloring of your application and this is completely optional.
Photon Pun 2 (free version)
Once the package is downloaded and imported it will open the "PUN Setup" where you should put your email and then set up your password in your browser on the Photon website with the email sent to you
Click "Create a New App" with the type as Photon Pun and then click create
Copy the App ID and then head back to your Unity Project and select "Window -> Photon Unity Networking -> Highlight Server Settings" and then paste the App ID into the correct spot to sync Photon with your project
Part 1: Creating Steam VR character with movement
By now you should have a project with both of the above packages installed and the settings updated to be a Steam VR project as well as having a Photon Pun App connected to your project. Next we will add a Steam VR player into the program and give it simple movement using the joystick.
Remove the Main Camera and replace it with the Steam player prefab (can be found by searching your assets)
You can change the hands of your character by finding the left/right hand game objects in the player and changing them to any prefab you want such as one of the other options in the Steam VR assets or a custom model
Next we need to map the left joystick control to an output we can use for movement
Select "window -> Steam VR Input" and select the "+" icon to add a new input. Name this new input "TouchPad" and give it a Vector 2 input. Change the localized string to also be "TouchPad" and select open binding UI
If your headset is not connected at this point you will not be able to bind anything so it is important to connect your headset by connecting to your personal machine via a cord or through virtual desktop (to YOUR machine)
Uncheck the "mirror action" box in the center of the screen to stop from mirroring your input on both controllers
Remove the "snap turn" action from the left controller joystick and add a new action of type "Joystick". Select the position section of this new action and select "TouchPad" inside of it to bind our new action to the left joystick
Now we must apply movement to the Steam VR Player using our new input
Add a "Character Controller" to your Steam VR Player (can be found by searching your assets)
Apply the "Player Controller" script that can be found at the bottom of this page in the scripts
Part 2: Multiplayer using Photon Pun
This section of the tutorial is easy to mess up so I would recommend following along with the second YouTube video linked at the bottom. It has good visuals and explanations that complement this written tutorial well.
Create a network manager to force players into a Photon room
Create a new empty game object named "Network Manager" and add the "Network Manager" script linked at the bottom of this page to this object as a C# script
Add the "Network Player Spawner" C# script to this object as well (linked below)
Create an avatar so that your spawner script will spawn physical players
Create a new empty game object named "Network Player" and add a PhotonView component to it in the inspector as well as the "Network Player" script linked below
Create 3 new empty game objects inside of the network player named "Head", "LeftHand" , and "RightHand" and add a "Photon Transform View" component to each of these new objects
Create a sphere and link it to head. Create a cube and resize it into long rod for your LeftHand and create a second cube for RightHand
Make your avatar a prefab to be used by the network player spawner
Create a folder in your assets folder named "Resources" (spelling is important)
Drag your Network Player from the inspector into this folder to make it a prefab
Delete your network player from the inspector so that it now only exists in the resources folder
Building the application for deployment to other computers:
Go to "File -> Build Settings" and select the PC option on the left. Then select the "Develop Build" checkbox and click "Build".
It will ask you to save your build somewhere so just put it somewhere that you will remember it
You can then zip this folder and send it to others as a download
All they have to do to run your program is:
Extract the folder
Connect their headset to their computer and launch Steam VR
Run the .exe file inside of the extracted folder
Scripts (dated March 2022):
Here is a link to a google drive folder containing the 4 scripts that you need for this tutorial: Tutorial Scripts. Go ahead and download them and add them to your Unity project as C# Scripts. If you have a Mac it may ask you to change the line endings which you should accept in order to suppress the warnings.
Helpful YouTube videos by Valem:
Valem is a YouTuber who has created lots of good content on creating VR games with Unity. Not every part of these videos was added in this tutorial. For instance the first part of "How to Make a VR Multiplayer Game - PART 1" is about developing for Oculus which we are not doing so it was skipped and replaced with the Steam VR set up.
Part 1: STEAM VR - The Ultimate VR developer guide - PART 1
Part 2: How to Make a VR Multiplayer Game - PART 1
Part 2 extended: How to Make a VR Multiplayer Game - PART 2
He has tutorials for developing for Oculus, Steam VR, and other platforms. He also does game reviews, tutorials on building different genres of games such as FPS or horror, and how to develop under certain constraints such as having no headset. If you have a basic question about VR and would like a tutorial, Valem's channel likely has an answer and a video for you to watch on the subject.
If you made it all of the way through this tutorial unscathed, congratulations! Networking is tricky and can lead to lots of bugs. If you are looking down here for help and haven't watched the YouTube videos yet make sure to go do that. If you are looking to add more features and don't know where to start there is a manual at this link, Photon Pun Manual, which has lots of good info on how to allow users to interact with the same objects and other fun things which could be beneficial to your project.