VRService
VRService is responsible for handling interactions between Roblox and Virtual Reality (VR). Its methods, properties, and events help you provide the best experience for end users seeking to experience Roblox on VR devices.
See VR Guidelines for more information on publishing an experience for VR devices.
Code Samples
The following example demonstrates how to get the Enum.UserCFrame of the left controller and place a part at that point in real world space. The CFrame of the controller changes whenever the device moves, so you should update the necessary parts whenever VRService.UserCFrameChanged fires.
local VRService = game:GetService("VRService")
local part = workspace.Part
local handOffset = VRService:GetUserCFrame(Enum.UserCFrame.LeftHand)
-- Account for headscale
handOffset = handOffset.Rotation + handOffset.Position * workspace.CurrentCamera.HeadScale
part.CFrame = workspace.CurrentCamera.CFrame * handOffset
Summary
Properties
Automatically adjusts scaling in VR to align the player with their avatar.
When true, a VR player will be able to animate their hands and head using their controllers and headset.
When true, a VR player's view will fade to black when their head collides with an object.
Describes what Enum.UserCFrame is responsible for input in VR.
Describes whether the user is using a virtual reality device.
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 Enum.UserCFrame is available to be listened to.
Re-centers the CFrame to the current location of the VR headset being worn by the user.
Requests navigation to the specified CFrame using the specified Enum.UserCFrame as the origin for the visualizer parabola.
Sets the mode of the specified Enum.VRTouchpad to the specified Enum.VRTouchpadMode.
Events
Fired when navigation is requested from VRService.
Fires if the Enum.VRTouchpadMode of a Enum.VRTouchpad is changed.
Fires when a Enum.UserCFrame is changed.
Fires when a Enum.UserCFrame is enabled or disabled.
Properties
AutomaticScaling
When set to Enum.VRScaling.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.
AvatarGestures
When set to true, a VR player will be able to animate their hands and head using their controllers and headset.
This property must be set on the server.
ControllerModels
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
This property describes what Enum.UserCFrame is responsible for input in VR. For instance, if a VR headset is responsible, the value of this property will be Enum.UserCFrame.Head.
To check if Roblox detects any VR devices, which would be responsible for input in VR, you can check the VREnabled property.
Code Samples
This example checks if Roblox detects a VR device. If a VR device is detected, this prints the name of the UserCFrame responsible for VR input. If not, this example prints "No VR device detected!".
local VRService = game:GetService("VRService")
if VRService.VREnabled then
print(VRService.GuiInputUserCFrame.Name)
else
print("No VR device detected!")
end
LaserPointer
ThirdPersonFollowCamEnabled
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 methods 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
This property can only be used in a LocalScript.
See Also
Code Samples
This example demonstrates how to implement a head tracking script that mirrors the movement of a virtual reality (VR) headset (the user's actual head) to their in-game character's head.
The example first check if the user is using a VR device by checking the value of the VREnabled() property. This example only works if the user is using a VR headset.
To determine the initial CFrame of the character's head, the code sample calls GetUserCFrame() and passes Enum.UserCFrame.Head as the argument.
To update the head's CFrame whenever the user's VR headset moves, the example connects the VRService.UserCFrameChanged event to the TrackHead() function. When the event fires to indicate that a VR device moved, TrackHead() checks if the headset moved. If the headset moved, the function updates the CFrame of the character's head to the CFrame value provided as an argument.
As the UserCFrame enum also tracks VR left and right hand devices, the concept of VR device tracking can be expanded to other character bodyparts.
In order for the example to work as expected, it must be placed in a LocalScript.
local VRService = game:GetService("VRService")
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 VRService.VREnabled then
-- Set the initial CFrame
head.CFrame = VRService:GetUserCFrame(Enum.UserCFrame.Head)
-- Track VR headset movement and mirror for character's head
VRService.UserCFrameChanged:Connect(TrackHead)
end
Methods
GetTouchpadMode
This method returns the Enum.VRTouchpadMode indicating the mode of a specified Enum.VRTouchpad. The returned mode indicates how the user interacts with their touchpad to play the game.
This can also be used alongside the several UserInputService VR methods and events.
This method will only work when used in a LocalScript.
Parameters
The specified VRTouchpad.
Returns
The mode of the specified VRTouchpad.
Code Samples
This example retrieves and prints the name of the user's current VRTouchpad.Left touchpad mode.
local VRService = game:GetService("VRService")
VRService:GetTouchpadMode(Enum.VRTouchpad.Left)
GetUserCFrame
This method returns a CFrame describing the position and orientation of a specified virtual reality (VR) device as an offset from a point in real world space. This method should be used when implementing VR compatibility into a game to obtain and track the movement of a connected VR device.
By using the method, developers 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.
VRService also provides a UserCFrameChanged event that automatically fires when the CFrame of connected VR device changes, so long it is used in a LocalScript.
This method will only work when used in a LocalScript.
Parameters
The specified UserCFrame.
Returns
Code Samples
This example positions a part at the player's left hand, assuming Camera.HeadLocked = true
local Workspace = game:GetService("Workspace")
local VRService = game:GetService("VRService")
local camera = Workspace.CurrentCamera
local part = script.Parent.Part
local handOffset = VRService:GetUserCFrame(Enum.UserCFrame.LeftHand)
-- Account for headscale
handOffset = handOffset.Rotation + handOffset.Position * camera.HeadScale
part.CFrame = camera.CFrame * handOffset
GetUserCFrameEnabled
This method returns true if the specified Enum.UserCFrame virtual reality device (VR) is available to be listened to. It can be used to determine whether a specified VR device, such as Enum.UserCFrame.Head, is connected to the user's game.
This can also be used alongside the several UserInputService VR methods and events.
This method 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
This example indicates whether the UserCFrame.Head VR device is enabled or disabled for the user. If the device is enabled, this prints "VR device is enabled!". If the device is disabled, this prints "VR device is disabled!".
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
This method re-centers the CFrame of the user's head to the current location of the VR headset being worn by the user. It 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 behaves identically to UserInputService:RecenterUserHeadCFrame().
This method will only work when used in a LocalScript.
Returns
Code Samples
This example fires the function to recenter the CFrame of the user's head to the current location of the VR headset being worn by the user.
local VRService = game:GetService("VRService")
VRService:RecenterUserHeadCFrame()
RequestNavigation
This method requests navigation to the specified CFrame using the specified Enum.UserCFrame as the origin for the visualizer parabola. It 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.
VRService has a similar event, NavigationRequested, used to detect such requests. This can also be used alongside the several UserInputService VR methods and events.
This method will only work when used in a LocalScript.
Parameters
The VR device for which the navigation is requested.
Returns
Code Samples
This example requests navigation from the user's UserCFrame.Head coordinates to the CFrame coordinates of a Part named NavigationDestination.
Note: In order for this to work, a Part named NavigationDestination must exist in the game's Workspace.
local VRService = game:GetService("VRService")
local destination = workspace:FindFirstChild("NavigationDestination")
VRService:RequestNavigation(Enum.UserCFrame.Head, destination.CFrame)
SetTouchpadMode
This method sets the mode of the specified Enum.VRTouchpad to the specified Enum.VRTouchpadMode. It can be used to change the user's virtual reality (VR) touchpad mode so that the user interacts with the game differently using the touchpad.
This can also be used alongside the several UserInputService VR methods and events.
This method 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
This example sets the user's VRTouchpad.Left touchpad mode to TouchMode.Touch. This means that the left touchpad is treated as ButtonB.
local VRService = game:GetService("VRService")
VRService:SetTouchpadMode(Enum.VRTouchpad.Left, Enum.VRTouchpadMode.Touch)
Events
NavigationRequested
This event fires when navigation is requested from VRService for a specified Enum.UserCFrame VR device. It fires with a CFrame coordinate and the specified Enum.UserCFrame indicating the device requesting the navigation.
This event can be used alongside UserInputService service events and methods.
Since this event fires locally, it can only be used in a LocalScript.
Parameters
Indicates the VR device for which navigation is requested.
Code Samples
This example prints the name of the UserCFrame VR device making the request, and the CFrame coordinates passed.
local VRService = game:GetService("VRService")
VRService.TouchpadModeChanged:Connect(function(cframe, inputUserCFrame)
print(inputUserCFrame.Name .. " made request with CFrame: " .. cframe)
end)
TouchpadModeChanged
This event fires if the Enum.VRTouchpadMode of a Enum.VRTouchpad is changed. You can use this event to track the states of VR touchpads connected via the user's client.
This event can be used alongside UserInputService service events and methods.
Since this event fires locally, it can only be used in a LocalScript.
Parameters
The touchpad that changed mode.
The new mode.
Code Samples
This example fires when the state of a VRTouchpad changes. It prints the name of the Touchpad that changed, and the state that it changed to.
local VRService = game:GetService("VRService")
VRService.NavigationRequested:Connect(function(pad, mode)
print(pad.Name .. " Touchpad changed to state: " .. mode.Name)
end)
UserCFrameChanged
This event fires when a Enum.UserCFrame is changed, for instance when the user moves a connected VR device. It can be used alongside GetUserCFrame() to track the CFrame coordinates of a VR device, and when it changes/moves. It can also be used alongside UserInputService service events and methods.
Since this event fires locally, it can only be used in a LocalScript.
Parameters
The type of VR device that changed.
Code Samples
This event fires when the user moves a connected VR device. When the event fires, this prints the name of the VR device type that changed, and the updated CFrame coordinates.
local VRService = game:GetService("VRService")
VRService.UserCFrameChanged:Connect(function(userCFrameType, cframeValue)
print(userCFrameType.Name .. " changed. Updated Frame: " .. tostring(cframeValue))
end)
UserCFrameEnabled
This event fires when a Enum.UserCFrame is enabled or disabled. It can be used alongside GetUserCFrameEnabled() to track whether a specified UserCFrame is enabled, and when its state changes. It can also be used alongside UserInputService service events and methods.
Since this 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
This example fires when a UserCFrame changes state, printing the name of the changed UserCFrame and whether it changed got enabled or disabled.
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)