Multiplayer VR Template

Multiplayer VR
Template
 
Environment
Unity 2020.3.21f1
You can probably use a newer version, but this is what the template was built in
Unity XR Interaction Toolkit (2.0.0-pre.6)
Photon Unity Networking (PUN, v2)
Photon Voice (v2)
Unity OpenXR Plugin (1.3.1)
To maximize platform compatibility
Other backends were causing problems
Unity XR Interaction Toolkit
Handles some complex tasks for you by implementing…
Direct interaction (grabbing)
Ray interaction (interaction from a distance)
Interactable objects
Locomotion (Teleportation, sliding, turning)
The XR Origin
Basic set of functionality for an XR player
Provides a camera and HMD tracking to place a user in your virtual world
Not very useful on it’s own
NetworkedPlayer is a modified XR Origin used by the template
Prefer to use the action-based XR prefabs
Input
Input doesn’t work by default
Window > Package Manager > XR Interaction Toolkit > Samples
Then import the Default Input Actions
Place an empty object in your scene and attach the 
Input Action Manager 
component
Add 
XRI Default Input Actions
 to the list of action assets
Input
Since we are using input actions, the Unity project will be using the 
new input system
i.e. Input.GetKeyDown() won’t work
The input actions give names to the tracking methods and buttons
A few of the important ones
Position
Rotation
Select (the trigger; index finger)
Activate (the grip button)
Turn (the trackpad)
Input
Do peripherals work (e.g. Vive trackers)?
… maybe?
Due to a bug on Valve/HTC’s end, trackers used to not work at all
This has potentially been fixed since January 11th
I’ve done some testing, but no success yet
The readme on GitHub for this template has some more info about this (in Other Remarks)
Interaction
Interactors
Things that interact with the environment
Usually your hands
Interactables
Things the user can interact with
Interaction Managers
Facilitates communication between interactors and interactables
You can have multiple, which lets you divide your interactions into distinct categories
Teleportation interaction manager
UI interaction manager
And whatever else you may need
Interaction
Interactors
XRDirectInteractor (direct interaction with your hands)
XRRayInteractor (interaction over a distance)
An XRInteractorLineVisual lets you see this ray
XRSocketInteractor (Hold an interactable in place in the world)
Interactables
XRGrabInteractable (follows an interactor around)
Grabbing with your hands
Following the trajectory of an XRRayInteractor
Interaction Managers
Facilitates communication between interactors and interactables
You can have multiple, which lets you divide your interactions into distinct categories
Teleportation interaction manager, UI interaction manager, etc
Interaction
A minimal grabbable interactable
RIgidbody
XRGrabInteractable
Select event should call TransferOwnership.transferOwnership()
TransferOwnership
InteractionManagerHook
PhotonView
PhotonRigidbodyView
PhotonTransformView
NetworkInstantiation
Locomotion
The toolkit provides components that do a lot of background work for locomotion
Locomotion system
Provides access to the XR Origin for locomotion providers
Locomotion providers
Determine how the user can update their position
Teleportation
Continuous movement (sliding along the floor smoothly)
Snap turning (Turn x degrees instantaneously)
Continuous turning (Smooth turing)
Locomotion
To use teleportation, we need teleportation areas
Place teleportation areas as children of your floor object
They should automatically inherit the shape of your floor
Ensure that teleportation areas are slightly higher than the floor
This lets the teleportation objects see the floor
A height difference of +0.001 units should work fine
A ray interactor and line visual is a good way to perform the
teleportation
Photon
A library that introduces networking capabilities in Unity
Players enter a “room” on the Photon server
A room is a set of players that may communicate together
Synchronizes the state of Unity GameObjects between multiple clients
Objects that need to be visible over the network must…
… have a PhotonView component
… be instantiated at runtime
Additional information may need to be synchronized
PhotonTransformView: synchronizes position and rotation
PhotonRigidbodyView: synchronizes a rigidbody (i.e. velocity)
PhotonAnimatorView: synchronizes animation state
Photon
To instantiate a prefab over the network with Photon, the prefab needs to be located in
the Assets/Resources directory
Photon searches this folder for prefab 
names
Instantiation options
PhotonNetwork.Instantiate()
Object will have the same lifespan as the client
PhotonNetwork.InstantiateRoomObject()
Object will have the same lifespan as the room
Photon will handle the destruction of objects that have exceeded their lifespan
Photon
Ownership
The owner is the client that is the authority for the object’s transform, physics, etc.
Transferring ownership
A PhotonView can change its owner
Remember to set 
Ownership Transfer 
to 
Takeover
 in the object’s PhotonView
Why?
Whoever owns a PhotonView will see the 
minimum amount of latency
 when interacting with
the GameObject the view is attached to
When should ownership be transferred?
Usually when a user picks up an object
When else?
XRGrabInteractable ->
Interactable Events -> 
Select
Photon
MonoBehaviourPun
An alternative to the MonoBehaviour class
Provides a built-in reference to the PhotonView of a GameObject
Set Ownership Transfer to 
Takeover
 to enable ownership transfer
Making things happen 
once
Since Photon synchronizes the state of GameObjects, the code you write will be
executed on 
all clients 
by default
Sometimes this will work, but there are other times where we want to make sure
Photon only runs code on a single client
Instantiating a network object, for example, should only happen once
To do this, run the code only if the client is the 
master client
The master client is usually the first client that joins
if (PhotonNetwork.IsMasterClient) { … }
Making things happen
 on other clients
Remote Procedure Calls (RPCs)
A function that gets executed on a client other than yours
Can be used to synchronize state that PhotonViews can’t handle
Can be used to transmit information, such as a chat message
Making things happen
 on other clients
 
The RPC Method
To make a method an RPC, simply place 
[PunRPC]
 on the line before the function
definition
Calling the RPC
Get a reference to the component’s PhotonView
Call PhotonView.RPC()
1st parameter: the name of the RPC function
2nd parameter: who should receive the RPC
3rd parameter and onward: parameters of the RPC
Consequences of runtime instantiation
Instantiating things at runtime means that the Unity editor cannot be leveraged to pass
references around
Instead, we use “hook scripts” that establish these references at runtime
Place it on a prefab with a component that needs a reference to something else
Provide the hook with the name of the object that will have that reference
Not a very elegant or scalable solution
Often used to provide interactables and interactors with references to their interaction
manager
Interacting with Menus
The NetworkedPlayer prefab also sets the scene up for
menus
The default NetworkedPlayer has a ray interactor that
interacts with menus on the left hand
First, create a menu as you normally would in Unity
Make sure to place it in world space
Then, add the following components to the Canvas object
A script to drive your menu interactions
An example is the Menu System script in the Template
scene
Tracked Device Graphic Raycaster
Event System
XRUI Input Module
The NetworkedPlayer Prefab
Instantiated at runtime by Instantiation.cs
Manages VR input
HMD position/rotation, controller position/rotation, controller buttons
Manages locomotion
Separates the VR logic from the pieces of the player that are visible over the network
Information that needs to be seen over the network is copied (once per frame) from the objects
that provide it into an object with appropriate PhotonViews
The NetworkedPlayer Prefab
The head
HMD tracking
Camera work
The hands
Interaction with the environment
Hand tracking
Locomotion
Networked representation
How other users see you
Voice connection
Automatically connects to a Photon server for voice
communication
Why separate the internal and networked parts?
The separation of the VR internals and the networked components came about due to a
problem between the architecture of SteamVR and Photon in an old version of the
template
The first user controlled themself 
and the second user
It made sense to make the two pieces distinct
This lets you have more control over what information is sent over the network at the cost
of writing a little more code
Makes it easier to prevent inputs from affecting all users
Networked Representations
The local user is unable to see the networked representation, but all other users can
The local user instead sees their own hands
Animations can also be synchronized to the networked representation
Copy/set the respective animation state
Use a PhotonAnimatorView to propagate the information over the network
For example, the NetworkedPlayer prefab copies the animation state of the “local hands” to the
networked representation of the hands
The NetworkedPlayer Prefab
A caveat of the Unity XR Interaction Toolkit: an object can
only have one interactor component
The Controller Manager combats this
Allows the player to cycle through different interaction
methods (“controllers”) on one hand
By default, you will see
a teleportation controller
a menu interaction controller
a grab controller
If you only need one type of interaction, you won’t need
this
Spawning the Player
Spawnpoint prefabs define a place in the world where the player will be placed initially
Place them in the world and assign them an index
Start from 0 and count upwards
The first player will spawn at index 0, the second at index 1, …
Circular: Will loop back to 0 when you run out of spawn points
Voice Communication
NetworkedPlayer also implements a connection to a server that
transmits voice
The object responsible for this can be found as seen to the right
But only at runtime!
The error below can be safely ignored
It has to do with race conditions related to connecting to our Photon
servers
Voice Communication
Voice transmission
The VoiceConnection prefab has a Transmit Enabled flag (under the Recorder)
When this flag is set, voice will transmit across the network
You can have it always on or create a script to facilitate push-to-talk
Voice Communication
The components involved
Photon Voice View
Recorder
Speaker
Voice Connection
Connect and Join
As well as an additional Enable Recorder script that automatically associates the
recorder with the voice connection and enables it
The main settings to worry about 
Voice Connection > App Setings > Server
Recorder > Transmit Enabled
Demonstration
A sample game has been made in the Ping Pong scene
Demonstrates interactions and ownership transfer
This should be one of the first places you look as a reference
There is also another scene (called Template) that shows how menus can be used
The Multiplayer Base scene contains the minimum components for multiplayer VR
A demo of the Ping Pong scene
Useful links
GitHub repository for the Multiplayer Template
Has installation instructions
Documentation for the Unity XR Interaction Toolkit
Documentation for Photon
Documentation for Photon Voice
Questions
Take a look through the template, particularly the NetworkedPlayer and Ping Pong Ball
prefabs
There’s a lot of moving parts that take a while to explain in depth
These prefabs should use most of the essential pieces of the template
Feel free to email me questions about the template at
erik.i.marsh@gmail.com
However, I will likely be able to explain things and walk you through issues more clearly in person
Slide Note
Embed
Share

This template provides a detailed overview of a multiplayer VR environment built using Unity XR Interaction Toolkit, Photon Unity Networking, and Unity OpenXR Plugin. It covers aspects like input actions, interaction handling, and locomotion systems, aiming to assist developers in creating immersive VR experiences. The content also addresses potential issues with peripherals, interaction managers, and XR player functionality.

  • Virtual Reality
  • Unity
  • XR Interaction
  • Multiplayer
  • VR Template

Uploaded on Feb 19, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Multiplayer VR Template

  2. Environment Unity 2020.3.21f1 You can probably use a newer version, but this is what the template was built in Unity XR Interaction Toolkit (2.0.0-pre.6) Photon Unity Networking (PUN, v2) Photon Voice (v2) Unity OpenXR Plugin (1.3.1) To maximize platform compatibility Other backends were causing problems

  3. Unity XR Interaction Toolkit Handles some complex tasks for you by implementing Direct interaction (grabbing) Ray interaction (interaction from a distance) Interactable objects Locomotion (Teleportation, sliding, turning) The XR Origin Basic set of functionality for an XR player Provides a camera and HMD tracking to place a user in your virtual world Not very useful on it s own NetworkedPlayer is a modified XR Origin used by the template Prefer to use the action-based XR prefabs

  4. Input Since we are using input actions, the Unity project will be using the new input system i.e. Input.GetKeyDown() won t work The input actions give names to the tracking methods and buttons A few of the important ones Position Rotation Select (the trigger; index finger) Activate (the grip button) Turn (the trackpad)

  5. Input Do peripherals work (e.g. Vive trackers)? maybe? Due to a bug on Valve/HTC s end, trackers used to not work at all This has potentially been fixed since January 11th I ve done some testing, but no success yet The readme on GitHub for this template has some more info about this (in Other Remarks)

  6. Interaction Interactors Interactables Things the user can interact with Interaction Managers Facilitates communication between interactors and interactables You can have multiple, which lets you divide your interactions into distinct categories Teleportation interaction manager UI interaction manager And whatever else you may need Things that interact with the environment Usually your hands

  7. Locomotion The toolkit provides components that do a lot of background work for locomotion Locomotion system Provides access to the XR Origin for locomotion providers Locomotion providers Determine how the user can update their position Teleportation Continuous movement (sliding along the floor smoothly) Snap turning (Turn x degrees instantaneously) Continuous turning (Smooth turing)

  8. Locomotion To use teleportation, we need teleportation areas Place teleportation areas as children of your floor object They should automatically inherit the shape of your floor Ensure that teleportation areas are slightly higher than the floor This lets the teleportation objects see the floor A height difference of +0.001 units should work fine A ray interactor and line visual is a good way to perform the teleportation

  9. Photon A library that introduces networking capabilities in Unity Players enter a room on the Photon server A room is a set of players that may communicate together Synchronizes the state of Unity GameObjects between multiple clients Objects that need to be visible over the network must have a PhotonView component be instantiated at runtime Additional information may need to be synchronized PhotonTransformView: synchronizes position and rotation PhotonRigidbodyView: synchronizes a rigidbody (i.e. velocity) PhotonAnimatorView: synchronizes animation state

  10. Photon To instantiate a prefab over the network with Photon, the prefab needs to be located in the Assets/Resources directory Photon searches this folder for prefab names Instantiation options PhotonNetwork.Instantiate() Object will have the same lifespan as the client PhotonNetwork.InstantiateRoomObject() Object will have the same lifespan as the room Photon will handle the destruction of objects that have exceeded their lifespan

  11. Photon Ownership Transferring ownership A PhotonView can change its owner Remember to set Ownership Transfer to Takeover in the object s PhotonView Why? Whoever owns a PhotonView will see the minimum amount of latency when interacting with the GameObject the view is attached to When should ownership be transferred? Usually when a user picks up an object When else? The owner is the client that is the authority for the object s transform, physics, etc. XRGrabInteractable -> Interactable Events -> Select

  12. Making things happen once Since Photon synchronizes the state of GameObjects, the code you write will be executed on all clients by default Sometimes this will work, but there are other times where we want to make sure Photon only runs code on a single client Instantiating a network object, for example, should only happen once To do this, run the code only if the client is the master client The master client is usually the first client that joins if (PhotonNetwork.IsMasterClient) { }

  13. Making things happen on other clients Remote Procedure Calls (RPCs) A function that gets executed on a client other than yours Can be used to synchronize state that PhotonViews can t handle Can be used to transmit information, such as a chat message

  14. Consequences of runtime instantiation Instantiating things at runtime means that the Unity editor cannot be leveraged to pass references around Instead, we use hook scripts that establish these references at runtime Place it on a prefab with a component that needs a reference to something else Provide the hook with the name of the object that will have that reference Not a very elegant or scalable solution Often used to provide interactables and interactors with references to their interaction manager

  15. Interacting with Menus The NetworkedPlayer prefab also sets the scene up for menus The default NetworkedPlayer has a ray interactor that interacts with menus on the left hand First, create a menu as you normally would in Unity Make sure to place it in world space Then, add the following components to the Canvas object A script to drive your menu interactions An example is the Menu System script in the Template scene Tracked Device Graphic Raycaster Event System XRUI Input Module

  16. The NetworkedPlayer Prefab Instantiated at runtime by Instantiation.cs Manages VR input HMD position/rotation, controller position/rotation, controller buttons Manages locomotion Separates the VR logic from the pieces of the player that are visible over the network Information that needs to be seen over the network is copied (once per frame) from the objects that provide it into an object with appropriate PhotonViews

  17. The NetworkedPlayer Prefab The head The hands Networked representation How other users see you Voice connection Automatically connects to a Photon server for voice communication HMD tracking Camera work Interaction with the environment Hand tracking Locomotion

  18. Why separate the internal and networked parts? The separation of the VR internals and the networked components came about due to a problem between the architecture of SteamVR and Photon in an old version of the template The first user controlled themself and the second user It made sense to make the two pieces distinct This lets you have more control over what information is sent over the network at the cost of writing a little more code Makes it easier to prevent inputs from affecting all users

  19. Networked Representations The local user is unable to see the networked representation, but all other users can The local user instead sees their own hands Animations can also be synchronized to the networked representation Copy/set the respective animation state Use a PhotonAnimatorView to propagate the information over the network For example, the NetworkedPlayer prefab copies the animation state of the local hands to the networked representation of the hands

  20. The NetworkedPlayer Prefab A caveat of the Unity XR Interaction Toolkit: an object can only have one interactor component The Controller Manager combats this Allows the player to cycle through different interaction methods ( controllers ) on one hand By default, you will see a teleportation controller a menu interaction controller a grab controller If you only need one type of interaction, you won t need this

  21. Spawning the Player Spawnpoint prefabs define a place in the world where the player will be placed initially Place them in the world and assign them an index Start from 0 and count upwards The first player will spawn at index 0, the second at index 1, Circular: Will loop back to 0 when you run out of spawn points

  22. Voice Communication NetworkedPlayer also implements a connection to a server that transmits voice The object responsible for this can be found as seen to the right But only at runtime! The error below can be safely ignored It has to do with race conditions related to connecting to our Photon servers

  23. Voice Communication Voice transmission The VoiceConnection prefab has a Transmit Enabled flag (under the Recorder) When this flag is set, voice will transmit across the network You can have it always on or create a script to facilitate push-to-talk

  24. Demonstration A sample game has been made in the Ping Pong scene Demonstrates interactions and ownership transfer This should be one of the first places you look as a reference There is also another scene (called Template) that shows how menus can be used The Multiplayer Base scene contains the minimum components for multiplayer VR A demo of the Ping Pong scene

  25. Useful links GitHub repository for the Multiplayer Template Has installation instructions Documentation for the Unity XR Interaction Toolkit Documentation for Photon Documentation for Photon Voice

  26. Questions Take a look through the template, particularly the NetworkedPlayer and Ping Pong Ball prefabs There s a lot of moving parts that take a while to explain in depth These prefabs should use most of the essential pieces of the template Feel free to email me questions about the template at erik.i.marsh@gmail.com However, I will likely be able to explain things and walk you through issues more clearly in person

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#