PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

GetUserCFrame

The GetUserCFrame function returns a DataType/CFrame describing the position and orientation of a specified Enum/UserCFrame virtual reality (VR) device. If the specified device is not connected, the function returns DataType/CFrame|CFrame.new().

For example, the code snippet below prints the CFrame of the user’s VR headset.

local userInputService = game:GetService("UserInputService")
local cframe = userInputService:GetUserCFrame(Enum.UserCFrame.Head)

print(cframe)

By using the function, players can implement features such as re-positioning the user’s in-game character corresponding to the location of a connected VR device. This can be done by changing the CFrame of the user’s in-game body parts to match the CFrame of the specified VR device using Enum/UserCFrame and DataType/CFrame value arguments passed by the event.

See also

  • UserInputService/UserCFrameChanged, an event which fires when the DataType/CFrame of a VR device changes
  • VRService, a service used to implement VR support

As this event only fires locally, it can only be used in a LocalScript.

Parameters

Name Type Default Description

The Enum/UserCFrame corresponding to the VR device

Returns

Return Type Summary

A DataType/CFrame describing the position and orientation of the specified VR device


Code Samples


VR Head Tracing

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 Player/Character|character’s head.

The example first check if the user is using a VR device by checking the value of the UserInputService/VREnabled|VREnabled property. This example only works if the user is using a VR headset.

To determine the initial DataType/CFrame of the character’s head, the code sample calls UserInputService/GetUserCFrame|GetUserCFrame and passes Enum/UserCFrame|Enum.UserCFrame.Head as the argument.

To update the head’s CFrame whenever the user’s VR headset moves, the example connects the UserInputService/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 DataType/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 UserInputService = game:GetService("UserInputService")

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 UserInputService.VREnabled then
	-- Set the inital CFrame
	head.CFrame = UserInputService:GetUserCFrame(Enum.UserCFrame.Head)
	
	-- Track VR headset movement and mirror for character's head
	UserInputService.UserCFrameChanged:Connect(TrackHead)
end