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 中調整縮放以對齊玩家與他們的虛擬人偶。
當真實時,VR 玩家可以使用控制器和耳機來動畫手和頭部。
當真實時,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 中負責輸入的。例個體、實例,如果虛擬實境頭戴裝置負責,此屬性的值將為 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 事件反應到 VR 裝置移動。
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 來實現。使用 UserCFrame 枚列和事件傳送的 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 裝置類型。
返回
一個是否指示指定的 VR 裝置是啟用()還是禁用()的 boolean 值。
範例程式碼
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 裝置。
返回
範例程式碼
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
這個方法將指定的 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 裝置時,此事件會發生。它可以與 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 。
一個是否指示是否啟用 UserCFrame 或禁用 true 的 boolean (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)