VRService

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

無法建立
服務

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.

VRService

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

概要

屬性

方法

屬性

AutomaticScaling

平行讀取

當設為 Enum.VRScaling.World 時,Camera.HeadScale 會調整世界的比例,讓虛擬人偶從世界的角度看到。小型頭像的玩家會認為環繞他們的物體比擁有大型頭像的玩家大。

AvatarGestures

平行讀取

當設為真實時,虛擬實境玩家將能使用控制器和耳機來動畫手和頭部。

此屬性必須在伺服器上設置。

平行讀取

FadeOutViewOnCollision

平行讀取

當真實時,頭部與物物件碰撞時,VR玩家的視野會淡化為黑色。這個屬性防止玩家在 VR 期間能夠穿過牆壁看到內容。預設值為真。

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!".

VRService.GuiInputUserCFrame

local VRService = game:GetService("VRService")
if VRService.VREnabled then
print(VRService.GuiInputUserCFrame.Name)
else
print("No VR device detected!")
end
平行讀取

ThirdPersonFollowCamEnabled

唯讀
未複製
平行讀取

VREnabled

唯讀
未複製
平行讀取

此屬性描述用戶是否使用虛擬現實(VR)裝置。

如果 VR 裝置已啟用,您可以通過方法,例如 UserInputService:GetUserCFrame() 來與其位置和移動互動。您也可以使用 UserInputService.UserCFrameChanged 事件反應到 VR 裝置移動。


local UserInputService = game:GetService("UserInputService")
local isUsingVR = UserInputService.VREnabled
if isUsingVR then
print("User is using a VR headset!")
else
print("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.

VR Head Tracking

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.

VRService:GetTouchpadMode

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

VRService:GetUserCFrame

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!".

VRService:GetUserCFrameEnabled

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.

VRService:RecenterUserHeadCFrame

local VRService = game:GetService("VRService")
VRService:RecenterUserHeadCFrame()

RequestNavigation

()

這個方法要求導航到指定的 CFrame 使用指定的 Enum.UserCFrame 作為視覺化弧的起源。它可以用來將虛擬現實(VR)融入您的遊戲,通過提供從使用者的VR設備到目的地的導航路徑的視覺化手段來實現。

VRService 有相似的事件,NavigationRequested,用於偵測這類請求。此外,還可以與多個 UserInputService VR方法和事件一起使用。

此方法只能在 LocalScript 中使用時才能運作。

參數

cframe: CFrame

指定的 CFrame 坐標。

預設值:""
inputUserCFrame: Enum.UserCFrame

要求導航的 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.

VRService:RequestNavigation

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.

VRService:SetTouchpadMode

local VRService = game:GetService("VRService")
VRService:SetTouchpadMode(Enum.VRTouchpad.Left, Enum.VRTouchpadMode.Touch)

活動

當從 VRService 要求導航到指定的 Enum.UserCFrame VR 裝置時,此事件會發生。它使用 CFrame 坐標發射,指定的 Enum.UserCFrame 表示要求導航的裝置。

此事件可與UserInputService一起使用。

由於此事件是在本地發射的,因此只能在 LocalScript 中使用。

參數

cframe: CFrame

請求的 CFrame 坐標。

inputUserCFrame: Enum.UserCFrame

指示要求導航的 VR 裝置。


範例程式碼

This example prints the name of the UserCFrame VR device making the request, and the CFrame coordinates passed.

VRService.NavigationRequested

local VRService = game:GetService("VRService")
VRService.TouchpadModeChanged:Connect(function(cframe, inputUserCFrame)
print(inputUserCFrame.Name .. " made request with CFrame: " .. cframe)
end)

TouchpadModeChanged

此事件會發生,如果 Enum.VRTouchpadModeEnum.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.

VRService.TouchpadModeChanged

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 裝置類型。

value: CFrame

更新後的 CFrame 虛擬實境裝置的坐標。


範例程式碼

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.

VRService.UserCFrameChanged

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

enabled: boolean

一個是否指示是否啟用 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.

VRService.UserCFrameEnabled

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)