VRService
The VRService is a service that is responsible for handling interactions between Roblox and Virtual Reality (VR). If you're interested in incorporating VR compatibility into your game, this is the service for you!
The primary purpose of this service is to allow for games to communication and cooperate with virtual reality decisions - such as VR headsets and controllers. It allows a LocalScript to perform different actions depending on the device, and in turn, helps developers provide the best experience for the end user seeking to experience Roblox using VR devices.
Some usages of this service include detecting whether a user has any connected VR devices, tracking when those devices move, and determining how those devices interact with the user's in-game controls. In order to detect user input, the service must look for a service event. For example, the service can detect VR device movement via events such as VRService.UserCFrameChanged.
The UserInputService also contains several events and functions useful for enhancing a player's VR experience in your game.
Since this service is client-side only, it will only work when used in a LocalScript. It will not work when used within a Script. Client-side only means that users in the game can only detect their own input
- and not the input of other users.
Code Samples
-- We must get the VRService before we can use it
local VRService = game:GetService("VRService")
-- A sample function providing one usage of UserCFrameChanged
local function userCFrameChanged(typ, value)
print(typ.Name .. " changed. Updated Frame: " .. value)
end
VRService.UserCFrameChanged:Connect(userCFrameChanged)
Summary
Properties
Automatically adjusts scaling in VR to align the player with their avatar
When true, a VR player's view will fade to black when their head collides with an object.
Describes what UserCFrame is responsible for input in VR.
Methods
Returns the VRTouchpadMode indicating the mode of a specified VRTouchpad.
Returns a CFrame describing the position & orientation of a specified virtual reality device as an offset from a point in real world space.
Returns true if the specified UserCFrame is available to be listened to.
Recenters the CFrame to the current location of the VR headset being worn by the user.
Requests navigation to the specified CFrame, using the specified UserCFrame as the origin for the visualizer parabola.
Sets the mode of the specified VRTouchpad to the specified VRTouchpadMode.
Events
Fired when navigation is requested from the VRService.
Fires if the VRTouchpadMode of a VRTouchpad is changed.
Fired when a UserCFrame is changed.
Fires when a UserCFrame gets enabled or disabled.
Properties
AutomaticScaling
When VRService.AutomaticScaling is set to World, Camera.HeadScale adjusts so that the scale of the world is seen from the avatar's perspective. A player with a small avatar will perceive the objects around them as larger than a player with a large avatar will.
FadeOutViewOnCollision
When true, a VR player's view fades to black when their head collides with an object. This property prevents players from being able to see through walls while in VR. The default value is true.
GuiInputUserCFrame
The GuiInputUserCFrame property describes what UserCFrame is responsible for input in VR. For instance, if a VR headset is responsible, the value of this property will be UserCFrame.Head.
To check if Roblox detects any VR devices, which would be responsible for input in VR, you can check the VRService.VREnabled property.
Since VRService only runs client-side, this property will only work when used in a LocalScript.
Code Samples
local VRService = game:GetService("VRService")
if VRService.VREnabled then
print(VRService.GuiInputUserCFrame.Name)
else
print("No VR device detected!")
end
VREnabled
This property describes whether the user is using a virtual reality (VR) device.
If a VR device is enabled, you can interact with its location and movement through functions such as UserInputService:GetUserCFrame(). You can also react to VR device movement using the UserInputService.UserCFrameChanged event.
local userInputService = game:GetService("UserInputService")local isUsingVR = userInputService.VREnabledif (isUsingVR) thenprint("User is using a VR headset!")elseprint("User is not using a VR headset!")end
As UserInputService isclient-side only, this property can only be used in a LocalScript.
See [this][1] article for VR best practices.
See also:
Code Samples
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local function TrackHead(inputType, value)
if inputType == Enum.UserCFrame.Head then
head.CFrame = value
end
end
if UserInputService.VREnabled then
-- Set the inital CFrame
head.CFrame = UserInputService:GetUserCFrame(Enum.UserCFrame.Head)
-- Track VR headset movement and mirror for character's head
UserInputService.UserCFrameChanged:Connect(TrackHead)
end
Methods
GetTouchpadMode
The GetTouchpadMode function returns the VRTouchpadMode indicating the mode of a specified VRTouchpad.
The returned mode indicates how the user interacts with their touchpad to play the game. For more information about the different types of modes, see the VRTouchpadMode page.
This can also be used alongside the several UserInputService VR functions and events.
Since VRService only runs client-side, this function will only work when used in a LocalScript.
Parameters
The specified VRTouchpad.
Returns
The mode of the specified VRTouchpad.
Code Samples
local VRService = game:GetService("VRService")
VRService:GetTouchpadMode(Enum.VRTouchpad.Left)
GetUserCFrame
The GetUserCFrame function returns a CFrame describing the position & orientation of a specified virtual reality (VR) device as an offset from a point in real world space.
This function should be used when implementing VR compatibility into a game to obtain and track the movement of a connected VR device.
By using the function, players can implement features such as re-positioning the user's in-game character corresponding to the location of a connected VR device. This can be done by changing the CFrame of the user's in-game character to match the CFrame of the specified VR device using the UserCFrame enum and CFrame value arguments passed by the event.
The VRService service also provides an event VRService.UserCFrameChanged that automatically fires when the CFrame of connected VR device changes - so long it is used in a LocalScript.
Since VRService only runs client-side, this function will only work when used in a LocalScript.
Parameters
The specified UserCFrame.
Returns
Code Samples
local VRService = game:GetService("VRService")
local handOffset = VRService:GetUserCFrame(Enum.UserCFrame.LeftHand)
-- account for headscale
handOffset = handOffset.Rotation + handOffset.Position * workspace.CurrentCamera.HeadScale
part.CFrame = workspace.CurrentCamera.CFrame * handOffset
GetUserCFrameEnabled
The GetUserCFrameEnabled function returns true if the specified UserCFrame virtual reality device (VR) is available to be listened to.
This can be used to determine whether a specified VR device, (e.g. UserCFrame.Head), is connected to the user's game. If the specified VR device is connected, is it enabled (true). Otherwise, it is disabled (false).
This can also be used alongside the several UserInputService VR functions and events.
Since VRService only runs client-side, this function will only work when used in a LocalScript.
Parameters
The specified type of VR device.
Returns
A boolean indicating whether the specified VR device is enabled (true) or disabled (false).
Code Samples
local VRService = game:GetService("VRService")
local isEnabled = VRService:GetUserCFrameEnabled(Enum.UserCFrame.Head)
if isEnabled then
print("VR device is enabled!")
else
print("VR device is disabled!")
end
RecenterUserHeadCFrame
The RecentUserHeadCFrame function recenters the CFrame of the user's head to the current location of the VR headset being worn by the user.
This function can be used to ensure that the user's in-game head is positioned according to the location of the user's VR headset.
This is similar to the UserInputService function, UserInputService:RecenterUserHeadCFrame().
Since VRServiceService only runs client-side, this function will only work when used in a LocalScript.
Returns
Code Samples
local VRService = game:GetService("VRService")
VRService:RecenterUserHeadCFrame()
RequestNavigation
The RequestNavigation function requests navigation to the specified CFrame, using the specified UserCFrame as the origin for the visualizer parabola.
This can be used to incorporate virtual reality (VR) into your game by providing a means to visualize a navigation path from the user's VR device to a destination.
The VRService has a similar event, VRService.NavigationRequested, used to detect such requests. This can also be used alongside the several UserInputService VR functions and events.
Since VRService only runs client-side, this function will only work when used in a LocalScript.
Parameters
The specified CFrame coordinates.
The VR device for which the navigation is requested.
Returns
Code Samples
local VRService = game:GetService("VRService")
local destination = workspace:FindFirstChild("NavigationDestination")
VRService:RequestNavigation(Enum.UserCFrame.Head, destination.CFrame)
SetTouchpadMode
The SetTouchpadMode function sets the mode of the specified VRTouchpad to the specified VRTouchpadMode.
This can be used to change the user's virtual reality (VR) touchpad mode so that the user interacts with the game different using the touchpad. For more information about the different types of modes, see the VRTouchpadMode page.
This can also be used alongside the several UserInputService VR functions and events.
Since VRService only runs client-side, this function will only work when used in a LocalScript.
Parameters
The specified VRTouchpad you want to set the mode of.
The mode you want to set the specified VRTouchpad to.
Returns
Code Samples
local VRService = game:GetService("VRService")
VRService:SetTouchpadMode(Enum.VRTouchpad.Left, Enum.VRTouchpadMode.Touch)
Events
NavigationRequested
The NavigationRequested event fires when navigation is requested from the VRService for a specified UserCFrame VR device.
This is fired with a CFrame coordinate and specified UserCFrame indicating the device requesting the navigation.
This VRService event can be used alongside UserInputService service events and functions.
Since the event fires locally, it can only be used in a LocalScript.
Parameters
The requested CFrame coordinates.
Indicates the VR device for which navigation is requested.
Code Samples
local VRService = game:GetService("VRService")
VRService.TouchpadModeChanged:Connect(function(cframe, inputUserCFrame)
print(inputUserCFrame.Name .. " made request with CFrame: " .. cframe)
end)
TouchpadModeChanged
The TouchpadModeChanged event fires if the VRTouchpadMode of a VRTouchpad is changed. This event indicates the VRTouchpad that changes, and its new state.
You can use this event to track the states of VRTouchpads connected via the user's client.
This VRService event can be used alongside UserInputService service events and functions.
Since the event fires locally, it can only be used in a LocalScript.
Parameters
The touchpad that changed mode.
The new mode.
Code Samples
local VRService = game:GetService("VRService")
VRService.NavigationRequested:Connect(function(pad, mode)
print(pad.Name .. " Touchpad changed to state: " .. mode.Name)
end)
UserCFrameChanged
The UserCFrameChanged even fires when a UserCFrame is changed. For instance, this event fires when the user moves a connected VR device.
This can be used alongside VRService:GetUserCFrame() to track the CFrame coordinates of a VR devices, and when it changes/moves. It can also be used alongside UserInputService service events and functions.
Since the event fires locally, it can only be used in a LocalScript.
Parameters
The type of VR device that changed.
The updated CFrame coordinates of the VR device after the change.
Code Samples
local VRService = game:GetService("VRService")
VRService.UserCFrameChanged:Connect(function(userCFrameType, cframeValue)
print(userCFrameType.Name .. " changed. Updated Frame: " .. tostring(cframeValue))
end)
UserCFrameEnabled
The UserCFrameEnabled event fires when a UserCFrame gets enabled or disabled.
This can be used alongside VRService:GetUserCFrameEnabled() to track whether a specified UserCFrame is enabled, and when its state changes. It can also be used alongside UserInputService service events and functions.
Since the event fires locally, it can only be used in a LocalScript.
Parameters
The UserCFrame getting enabled or disabled.
A boolean indicating whether the UserCFrame is enabled (true) or disabled (false).
Code Samples
local VRService = game:GetService("VRService")
VRService.UserCFrameEnabled:Connect(function(type, enabled)
if enabled then
print(type.Name .. " got enabled!")
else
print(type.Name .. " got disabled!")
end
end)