VRService
*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.
Il servizio VRService è responsabile per la gestione delle interazioni tra Roblox e la realtà virtuale (VR).I suoi metodi, le proprietà e gli eventi ti aiutano a fornire la migliore esperienza per gli utenti finali che cercano di sperimentare Roblox su dispositivi VR.
Vedi Linee guida VR per ulteriori informazioni sulla pubblicazione di un'esperienza per dispositivi VR.
Campioni di codice
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
Sommario
Proprietà
Adegua automaticamente la dimensione in VR per allineare il giocatore con il suo Avatar.
Quando è vero, un giocatore VR sarà in grado di animare le proprie mani e la testa utilizzando i loro controller e le cuffie.
Quando è vero, la vista di un GiocatoreVR sbiadirà a nero quando la loro testa si scontra con un oggetto.
Descrive ciò che Enum.UserCFrame è responsabile per l'input in VR.
Descrive se l'utente sta utilizzando un Dispositivodi realtà virtuale.
Metodi
Restituisce il VRTouchpadMode che indica il modo di un VRTouchpad specificato.
Restituisce un CFrame che descrive la posizione e l'orientamento di un dispositivo di realtà virtuale specificato come offset da un punto nello spazio del mondo Spazio.
Restituisce vero se il Enum.UserCFrame è disponibile per essere ascoltato.
Ricentra il CFrame alla posizione attuale del casco VR indossato dall'utente.
Navigazione delle richieste allo specifico CFrame utilizzando lo specifico Enum.UserCFrame come origine per la parabola visualizzatore.
Imposta il modo del modello specificato Enum.VRTouchpad a quello specificato Enum.VRTouchpadMode.
Eventi
Spedito quando viene richiesta la navigazione da VRService .
Si attiva se il Enum.VRTouchpadMode di un Enum.VRTouchpad viene cambiato.
Si accende quando un Enum.UserCFrame viene cambiato.
Si attiva quando un Enum.UserCFrame è abilitato o disabilitato.
Proprietà
AutomaticScaling
Quando è impostato su Enum.VRScaling.World , Camera.HeadScale regola in modo che la scala del mondo sia vista dalla prospettiva dell'Avatar.Un giocatore con un piccolo avatar percepirà gli oggetti intorno a loro più grandi di quanto percepirà un giocatore con un avatar grande.
AvatarGestures
Quando è impostato su vero, un giocatore VR sarà in grado di animare le proprie mani e la testa utilizzando i loro controller e le cuffie.
Questa proprietà deve essere impostata sul Server.
ControllerModels
FadeOutViewOnCollision
Quando è vero, la vista di un GiocatoreVR sbiadisce a nero quando la loro testa si scontra con un oggetto.Questa proprietà impedisce ai giocatori di essere in grado di vedere attraverso le pareti mentre sono in VR.Il valore predefinito è vero.
GuiInputUserCFrame
Questa proprietà descrive ciò che Enum.UserCFrame è responsabile per l'input in VR.Ad esempio, se un casco VR è responsabile, il valore di questa proprietà sarà Enum.UserCFrame.Head .
Per controllare se Roblox rileva eventuali dispositivi VR, che sarebbero responsabili dell'input in VR, puoi controllare la ProprietàVREnabled.
Campioni di codice
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
Questa proprietà descrive se l'utente sta utilizzando un Dispositivodi realtà virtuale (VR).
Se un dispositivo VR è abilitato, puoi interagire con la sua posizione e il suo movimento attraverso metodi come UserInputService:GetUserCFrame() .Puoi anche reagire al movimento del dispositivo VR utilizzando l'evento 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
Questa proprietà può essere utilizzata solo in un LocalScript .
Vedi anche
Campioni di codice
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
Metodi
GetTouchpadMode
Questo metodo restituisce il Enum.VRTouchpadMode che indica il modo di un Enum.VRTouchpad specificato.La modalità restituita indica come l'utente interagisce con il touchpad per giocare.
Questo può essere utilizzato anche insieme ai diversi UserInputService metodi e eventi VR.
Questo metodo funzionerà solo quando viene utilizzato in un LocalScript .
Parametri
Il VRTouchpad .
Restituzioni
La modalità del VRTouchpad .
Campioni di codice
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
Questo metodo restituisce un CFrame che descrive la posizione e l'orientamento di un dispositivo di realtà virtuale specificato come offset da un punto nello Spaziodel mondo reale.Questo metodo deve essere utilizzato quando si implementa la compatibilità VR in un gioco per ottenere e tracciare il movimento di un DispositivoVR connesso.
Usando il metodo, gli sviluppatori possono implementare funzionalità come il riposizionamento del personaggio in gioco dell'utente corrispondente alla posizione di un DispositivoVR connesso.Questo può essere fatto cambiando il CFrame del personaggio in gioco dell'utente per abbinare il CFrame del dispositivo VR specificato utilizzando gli argomenti di valore UserCFrame e CFrame passati dall'evento.
VRService fornisce anche un evento UserCFrameChanged che si attiva automaticamente quando il CFrame del dispositivo VR connesso cambia, fintanto che viene utilizzato in un LocalScript .
Questo metodo funzionerà solo quando viene utilizzato in un LocalScript .
Parametri
Il UserCFrame .
Restituzioni
Campioni di codice
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
Questo metodo restituisce vero se il dispositivo di realtà virtuale specificato Enum.UserCFrame è disponibile per essere ascoltato.Può essere utilizzato per determinare se un DispositivoVR specificato, come Enum.UserCFrame.Head , è connesso al Giocodell'utente.
Questo può essere utilizzato anche insieme ai diversi UserInputService metodi e eventi VR.
Questo metodo funzionerà solo quando viene utilizzato in un LocalScript .
Parametri
Il tipo specificato di DispositivoVR.
Restituzioni
Un booleano che indica se il dispositivo VR specificato è abilitato ( true ) o disabilitato ( false ).
Campioni di codice
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
Questo metodo riposiziona il CFrame della testa dell'utente nella posizione attuale del casco VR indossato dall'utente.Può essere utilizzato per garantire che la testa dell'utente in gioco sia posizionata in base alla posizione del casco VR dell'utente.
Questo si comporta identicamente a UserInputService:RecenterUserHeadCFrame() .
Questo metodo funzionerà solo quando viene utilizzato in un LocalScript .
Restituzioni
Campioni di codice
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
Questo metodo richiede la navigazione verso il specificato CFrame utilizzando il specificato Enum.UserCFrame come origine per la parabola visualizzatore.Può essere utilizzato per incorporare la realtà virtuale (VR) nel tuo gioco fornendo un mezzo per visualizzare un percorso di navigazione da un dispositivo VR dell'utente a una destinazione.
VRService ha un evento simile, NavigationRequested , utilizzato per rilevare tali richieste.Questo può essere utilizzato anche insieme ai diversi UserInputService metodi e eventi VR.
Questo metodo funzionerà solo quando viene utilizzato in un LocalScript .
Parametri
Il dispositivo VR per il quale è richiesta la navigazione.
Restituzioni
Campioni di codice
Questo esempio richiede la navigazione dalle coordinate dell'utente UserCFrame.Head alle coordinate di CFrame di un Part chiamato NavigationDestination.
Nota: Per far funzionare questo, deve esistere una parte chiamata Destinazione di navigazione nell'area di lavoro del gioco.
local VRService = game:GetService("VRService")
local destination = workspace:FindFirstChild("NavigationDestination")
VRService:RequestNavigation(Enum.UserCFrame.Head, destination.CFrame)
SetTouchpadMode
Questo metodo imposta il modo del modello specificato Enum.VRTouchpad a quello specificato Enum.VRTouchpadMode.Può essere utilizzato per cambiare la modalità del touchpad della realtà virtuale (VR) dell'utente in modo che l'utente interagisca con il gioco in modo diverso utilizzando il touchpad.
Questo può essere utilizzato anche insieme ai diversi UserInputService metodi e eventi VR.
Questo metodo funzionerà solo quando viene utilizzato in un LocalScript .
Parametri
Il VRTouchpad desideri impostare la modalità.
La modalità in cui vuoi impostare il VRTouchpad a.
Restituzioni
Campioni di codice
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)
Eventi
NavigationRequested
Questo evento si attiva quando viene richiesta la navigazione da VRService per un DispositivoVR specificato Enum.UserCFrame.Spara con una coordinata CFrame e l'indicata Enum.UserCFrame indicando la richiesta del dispositivo che richiede la navigazione.
Questo evento può essere utilizzato insieme agli eventi e ai metodi del servizio UserInputService.
Poiché questo evento si attiva localmente, può essere utilizzato solo in un LocalScript .
Parametri
Indica il dispositivo VR per il quale è richiesta la navigazione.
Campioni di codice
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
Questo evento si attiva se il Enum.VRTouchpadMode di un Enum.VRTouchpad viene cambiato.Puoi utilizzare questo evento per tracciare gli stati dei touchpad VR connessi tramite il client dell'utente.
Questo evento può essere utilizzato insieme agli eventi e ai metodi del servizio UserInputService.
Poiché questo evento si attiva localmente, può essere utilizzato solo in un LocalScript .
Parametri
Il touchpad che ha cambiato modalità.
La nuova modalità.
Campioni di codice
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
Questo evento si attiva quando un Enum.UserCFrame viene cambiato, ad esempio quando l'utente sposta un DispositivoVR connesso.Può essere utilizzato insieme a GetUserCFrame() per tracciare le coordinate CFrame di un DispositivoVR e quando cambia/si muove.Può essere utilizzato anche insieme agli eventi e ai metodi del servizio UserInputService.
Poiché questo evento si attiva localmente, può essere utilizzato solo in un LocalScript .
Parametri
Il tipo di dispositivo VR che è cambiato.
Campioni di codice
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
Questo evento si attiva quando un Enum.UserCFrame è abilitato o disabilitato.Può essere utilizzato insieme a GetUserCFrameEnabled() per tracciare se un UserCFrame specificato è abilitato e quando cambia lo stato.Può essere utilizzato anche insieme agli eventi e ai metodi del servizio UserInputService.
Poiché questo evento si attiva localmente, può essere utilizzato solo in un LocalScript .
Parametri
Il UserCFrame ottenere abilitato o disabilitato.
Un booleano che indica se il UserCFrame è abilitato ( true ) o disabilitato ( false ).
Campioni di codice
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)