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

UserCFrameChanged

The UserCFrameChanged event fires when the DataType/CFrame of a VR device changes.

This event can be used to track the movement of a connected VR device.

Using the event, you can implement features such as moving the user’s in-game character limbs as the user moves their VR device. This can be done by changing the CFrame of the user’s in-game limbs to match the CFrame changes of the VR device using the Enum/UserCFrame enum and CFrame value arguments passed by the event.

To retrieve the DataType/CFrame of a connected VR device, use UserInputService/GetUserCFrame.

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

See also

  • VRService, used to implement support, including an identical event VRService/UserHeadCFrameChanged
  • Camera/HeadLocked, when this property is true the Camera will automatically track the head motion of a player using a VR device
  • Camera/GetRenderCFrame, a function which retrieves the DataType/CFrame the Camera is being orientated at, including the impact of VR devices

Parameters

Name Type Default Description

A Enum/UserCFrame value indicating which body part moved

A DataType/CFrame value indicating the updated CFrame of the body part that moved


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