VRService 负责处理 Roblox 和虚拟现实(VR)之间的互动。其方法、属性和事件可帮助您为寻求在 VR 设备上体验 Roblox 的终端用户提供最佳体验。
请参阅VR 指南获取更多关于为 VR 设备发布体验的信息。
代码示例
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
概要
属性
自动调整 VR 中的缩放以将玩家与其虚拟形象对齐。
当真实时,虚拟现实玩家可以使用控制器和头戴设备来动画手和头部。
当真实时,虚拟现实玩家的视图在他们的头部与物对象碰撞时会渐变黑色。
描述什么 Enum.UserCFrame 对 VR 的输入负责。
描述用户是否使用虚拟现实设备。
方法
返回指示特定 VRTouchpad 模式的 VRTouchpadMode。
返回描述特定虚拟现实设备位置和方向的 CFrame,作为从现实世界空间的某个点的抵消。
如果指定的 Enum.UserCFrame 可用以进行收听,返回真值。
将 CFrame 重新定位到用户正在穿戴的 VR 头戴设备的当前位置。
使用指定的 CFrame 作为视觉化弧的起源,请求导航到指定的 Enum.UserCFrame 以显示视觉化弧。
将指定的 Enum.VRTouchpad 设置为指定的 Enum.VRTouchpadMode 模式。
活动
在 VRService 从导航请求时发射。
如果 Enum.VRTouchpadMode 的 Enum.VRTouchpad 被更改,就会发生火灾。
当 Enum.UserCFrame 被更改时发生火灾。
当 Enum.UserCFrame 启用或禁用时发生火焰。
属性
AutomaticScaling
当设置为 Enum.VRScaling.World 时,Camera.HeadScale 将调整为使世界的比例从虚拟形象的视角看到。拥有小型虚拟形象的玩家会认为周围的物体比拥有大型虚拟形象的玩家更大。
ControllerModels
GuiInputUserCFrame
该属性描述了什么 Enum.UserCFrame 对 VR 的输入负责。例实例,如果 VR 头戴设备负责,该属性的值将为 Enum.UserCFrame.Head 。
要检查 Roblox 是否检测到任何 VR 设备,这将负责在 VR 中输入,您可以检查 VREnabled 属性。
代码示例
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
该属性描述用户是否使用虚拟现实(VR)设备。
如果启用了 VR 设备,您可以通过方法如 UserInputService:GetUserCFrame() 与其位置和移动进行互动。您还可以使用 UserInputService.UserCFrameChanged 事件反应虚拟现实设备的移动。
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
此属性只能在 LocalScript 中使用。
还看到也看到
代码示例
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
方法
GetTouchpadMode
该方法返回 Enum.VRTouchpadMode 指示特定 Enum.VRTouchpad 的模式。返回的模式表示用户如何与触摸板互动以玩游戏。
它也可以与几个 UserInputService VR方法和事件一起使用。
此方法仅在 LocalScript 中使用时才会工作。
参数
指定的 VRTouchpad 。
返回
指定的 VRTouchpad 的模式。
代码示例
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
该方法返回一个 CFrame 描述特定虚拟现实 (VR) 设备位置和方向的偏移,作为实世空间的一点。在实现 VR 兼容性到游戏时,应使用此方法来获取并跟踪连接的 VR 设备的移动。
通过使用方法,开发人员可以实现功能,例如重新定位连接到 VR 设备的位置的用户游戏角色。这可以通过将用户游戏中角色的 CFrame 更改为匹配指定 VR 设备的 CFrame 来实现。使用用户 CFrame 枚列和事件传递的 CFrame 值参数传递的值来匹配指定的 VR 设备的 CFrame 。
VRService 还提供一个 UserCFrameChanged 事件,当连接的 VR 设备的 CFrame 发生变化时自动触发,只要在 LocalScript 中使用,就可以了。
此方法仅在 LocalScript 中使用时才会工作。
参数
指定的 UserCFrame 。
返回
代码示例
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
该方法如果指定的 Enum.UserCFrame 虚拟现实设备 (VR) 可用于收听,将返回真值。它可以用来确定是否有特定的 VR 设备,例如 Enum.UserCFrame.Head,连接到用户的游戏。
它也可以与几个 UserInputService VR方法和事件一起使用。
此方法仅在 LocalScript 中使用时才会工作。
参数
指定的 VR 设备类型。
返回
一个 boolean 指示是否启用指定的 VR 设备 ( true ) 或禁用 ( false )。
代码示例
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
该方法将用户头部的 CFrame 重新定位到用户正穿戴的 VR 头戴设备的当前位置。它可以用于确保用户在游戏中的头部按照用户的 VR 头戴设备的位置进行定位。
这与 UserInputService:RecenterUserHeadCFrame() 相同。
此方法仅在 LocalScript 中使用时才会工作。
返回
代码示例
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
该方法要求使用指定的 CFrame 将导航请求发送到指定的 Enum.UserCFrame 作为视觉化球面的起源,以便执行视觉化计算。它可以用于将虚拟现实(VR)融入到您的游戏中,通过提供从用户的 VR 设备到目的地的导航路径的视觉化手段来实现。
VRService 有一个类似的事件,NavigationRequested,用于检测这样的请求。它也可以与几个 UserInputService VR 方法和事件一起使用。
此方法仅在 LocalScript 中使用时才会工作。
参数
导航请求的 VR 设备。
返回
代码示例
这个例子要求从用户的 UserCFrame.Head 坐标到命名为 NavigationDestination 的 CFrame 坐标进行导航。
注意: 为了使此功能正常运行,游戏工作区中必须存在名为 导航目的地 的部分。
local VRService = game:GetService("VRService")
local destination = workspace:FindFirstChild("NavigationDestination")
VRService:RequestNavigation(Enum.UserCFrame.Head, destination.CFrame)
SetTouchpadMode
该方法将指定的 Enum.VRTouchpad 设置为指定的 Enum.VRTouchpadMode 模式。它可以用于更改用户的虚拟现实(VR)触摸板模式,以便用户使用触摸板与游戏进行不同的互动。
它也可以与几个 UserInputService VR方法和事件一起使用。
此方法仅在 LocalScript 中使用时才会工作。
参数
指定的 VRTouchpad 你想设置的模式。
你想将指定的 VRTouchpad 设置为所需模式。
返回
代码示例
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)
活动
NavigationRequested
当要求导航到 VRService 为指定的 Enum.UserCFrame VR 设备时,此事件会触发。它使用 CFrame 坐标射击,指定的 Enum.UserCFrame 表示要求导航的设备。
该事件可与UserInputService一起使用。
由于此事件是在本地发生的,它只能在 LocalScript 中使用。
参数
指示需要导航的 VR 设备。
代码示例
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
此事件会触发,如果 Enum.VRTouchpadMode 的 Enum.VRTouchpad 被更改。您可以使用此事件来跟踪连接到用户客户端的 VR 触摸板的状态。
该事件可与UserInputService一起使用。
由于此事件是在本地发生的,它只能在 LocalScript 中使用。
参数
改变模式的触摸板。
新模式。
代码示例
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
当用户移动连接的 VR 设备时,例如当用户移动连接的 VR 设备时,此事件会触发,例如当用户移动连接的 VR 设备时,此事件会触发,例如当用户移动连接的 VR 设备时,此事件会触发它可以与 GetUserCFrame() 一起用于跟踪 VR 设备的 CFrame 坐标,当它更改/移动时。它还可以与 UserInputService 服务事件和方法并行使用。
由于此事件是在本地发生的,它只能在 LocalScript 中使用。
参数
更改的 VR 设备类型。
代码示例
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
当 Enum.UserCFrame 启用或禁用时,该事件会触发。它可以与 GetUserCFrameEnabled() 一起使用,以跟踪是否启用了指定的 UserCFrame 以及状态何时发生变化。它还可以与 UserInputService 服务事件和方法并行使用。
由于此事件是在本地发生的,它只能在 LocalScript 中使用。
参数
UserCFrame 正在启用或禁用。
一个 bool 指示 whether UserCFrame 是启用的 ( true ) 还是禁用的 ( false )。
代码示例
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)