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
AvatarGestures
ControllerModels
FadeOutViewOnCollision
GuiInputUserCFrame
Code Samples
local VRService = game:GetService("VRService")
if VRService.VREnabled then
print(VRService.GuiInputUserCFrame.Name)
else
print("No VR device detected!")
end
LaserPointer
ThirdPersonFollowCamEnabled
VREnabled
Code Samples
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
Parameters
Returns
Code Samples
local VRService = game:GetService("VRService")
VRService:GetTouchpadMode(Enum.VRTouchpad.Left)
GetUserCFrame
Parameters
Returns
Code Samples
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
Parameters
Returns
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
Returns
Code Samples
local VRService = game:GetService("VRService")
VRService:RecenterUserHeadCFrame()
RequestNavigation
Parameters
Returns
Code Samples
local VRService = game:GetService("VRService")
local destination = workspace:FindFirstChild("NavigationDestination")
VRService:RequestNavigation(Enum.UserCFrame.Head, destination.CFrame)
SetTouchpadMode
Parameters
Returns
Code Samples
local VRService = game:GetService("VRService")
VRService:SetTouchpadMode(Enum.VRTouchpad.Left, Enum.VRTouchpadMode.Touch)
Events
NavigationRequested
Parameters
Code Samples
local VRService = game:GetService("VRService")
VRService.TouchpadModeChanged:Connect(function(cframe, inputUserCFrame)
print(inputUserCFrame.Name .. " made request with CFrame: " .. cframe)
end)
TouchpadModeChanged
Parameters
Code Samples
local VRService = game:GetService("VRService")
VRService.NavigationRequested:Connect(function(pad, mode)
print(pad.Name .. " Touchpad changed to state: " .. mode.Name)
end)
UserCFrameChanged
Parameters
Code Samples
local VRService = game:GetService("VRService")
VRService.UserCFrameChanged:Connect(function(userCFrameType, cframeValue)
print(userCFrameType.Name .. " changed. Updated Frame: " .. tostring(cframeValue))
end)
UserCFrameEnabled
Parameters
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)