VRService 負責處理 Roblox 和虛擬實境 (VR) 之間的互動。它的方法、屬性和事件協助您為尋找 VR 設備的用戶提供最佳體驗。
有關發布 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 輸入的負荷。
描述使用者是否使用虛擬實境裝置。
方法
返回 VRTouchpadMode,表示指定的 VRTouchpad 的模式。
返回一個 CFrame 描述指定虛擬實境裝置位置和方向的偏移空間。
如果指定的 Enum.UserCFrame 可用來聆聽,則返回為 true。
將 CFrame 重新中心到使用者目前穿戴的 VR 耳機位置。
使用指定的 CFrame 導航到視覺化器參數。
將指定的 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 事件來反應 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 裝置的位置相符。這可以通過變更用戶在遊戲中的角色 CFrame 來達到。這可以通過使用 UserCFrame 枚 CFrame 值參數傳送給事件。
VRService 也提供一個 UserCFrameChanged 事件,當連接的 VR 裝置的 CFrame 變更時會自動發生,提供在 0> Class.LocalScript0> 中使用。
此方法只適用於 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
此方法會返回 true 如果指定的 Enum.UserCFrame 虛擬實境裝置可用來聆聽。它可以用於確認是否連接到用戶的遊戲,例如 Enum.UserCFrame.Head 。
這也可以和多個 UserInputService VR 方法和事件一起使用。
此方法只適用於 LocalScript 中使用。
參數
指定的 VR 裝置類型。
返回
指定的 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 的頭頂重新導至使用者的頭部所在位置,以確保使用者的遊戲頭戴按鈕頂部位的位置。它可以用於確保使用者的遊戲頭戴按鈕頂部位的位置。
這會相同於 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 。它可以用來變更使用者使用 touchpad 的虛擬實境 (VR) 模式,以便使用者與遊戲交互使用 touchpad。
這也可以和多個 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 坐標和指定的 1> Container.UserCFrame1> 來表示裝置要求的導航。
這個事件可以與 UserInputService 服務事件和方法結合。
這個事件只能在 LocalScript 中發生,因此它只能在 Class.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.VRTouch 已變更。您可以使用此事件來跟蹤連接到用戶端的 VR 觸摸板。
這個事件可以與 UserInputService 服務事件和方法結合。
這個事件只能在 LocalScript 中發生,因此它只能在 Class.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
這個事件會在 Enum.UserCFrame 變更時發生,例如當使用者移動連接的 VR 裝置時。它可以與 GetUserCFrame() 一起使用,以追蹤 VR 裝置的 CFrame 坐標,並且在
這個事件只能在 LocalScript 中發生,因此它只能在 Class.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 ,並且當其狀態
這個事件只能在 LocalScript 中發生,因此它只能在 Class.LocalScript 中使用。
參數
UserCFrame|UserCFrame 獲得啟用或關閉。
指示 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)