How do you get the exact position of the controllers in VR?

Roblox has horrible documentation for the VR API, I just want to figure out how to get the CFrame of the controller, like the one that’s shown in the game. Everything I’ve done so far doesn’t seem to allow me to get the same position of the controllers in game.

9 Likes

This might be what you need?
http://wiki.roblox.com/index.php?title=API:Class/VRService/GetUserCFrame

1 Like

Nope, that only gives me the position of the controllers relitive to 0,0,0
not the actual position of the controllers in the workspace.

1 Like

Try this then

((game.Workspace.CurrentCamera.CFrame+(game:GetService("VRService"):GetUserCFrame(Enum.UserCFrame.LeftHand).p)*game.Workspace.CurrentCamera.HeadScale).p)

1 Like

If you want the exact position where Roblox places the camera and controllers in VR you will have to look through the scripts they’ve made. As for VR in general, everything is based on an arbitrary offset, the only thing you can know for sure is where the controllers are relative to your headset (at least right now). You will have to place everything relative to the headset, and then move the headset with the character and so on.

1 Like

Roblox doesn’t give the information you want. You have to do it relative to the head.

Don’t ask why. My guess is it’s how the Steam API works.

1 Like

Other engines (UE4, Unity) have floor height api for every SteamVR and Oculus headset, so I’m assuming the API does exist. Roblox just doesn’t provide it on their end for some reason.

1 Like

Hm, good point - I was referring to how the controller positions are locally relative to the headset

1 Like

Hi, sorry for necro, but this is important code that everyone doing VR should know.

HeadScale = 1
Cam = workspace.CurrentCamera
Cam.HeadScale = HeadScale

local cfRH = VRService:GetUserCFrame(Enum.UserCFrame.RightHand)
RightHand.CFrame = (Cam.CFrame*CFrame.new(cfRH.p*HeadScale))*CFrame.fromEulerAnglesXYZ(cfRH:ToEulerAnglesXYZ())

this gives full 3d tracking relative to the Headset.
the controllers are not positioned relative to the headset, they’re relative to the Camera CFrame.
refer to this diagram

EDIT: For those of you finding this thread years after its posted. here’s a video tutorial: https://www.youtube.com/watch?v=WIIAegsfJ98

44 Likes

better version

rhrot = cfRH - cfRH.p
camrot = cam.CFrame - cam.CFrame.p
RightHand.CFrame =  camrot * rhrot + cam.CFrame*(cfRH.p*HeadScale)

or in one line

RightHand.CFrame = (cam.CFrame - cam.CFrame.p) * (cfRH - cfRH.p) + cam.CFrame*(cfRH.p*HeadScale)

or

RightHand.CFrame = cam.CFrame*CFrame.new(cfRH.p*HeadScale) * (cfRH - cfRH.p) 

wanted to make it simpler and utilize the vector mult operator

10 Likes

How do I use this for the left hand too? It just breaks

1 Like

You should not be using Camera.CFrame for getting the camera’s CFrame while in VR. You should be using Camera:GetRenderCFrame instead.
I find this code to work for me:

local headCFrame = VRService:GetUserCFrame(Enum.UserCFrame.Head)
local centerCFrame = headCFrame:ToObjectSpace(CameraHandler.Camera:GetRenderCFrame())
local leftHandCFrame = centerCFrame * VRService:GetUserCFrame(Enum.UserCFrame.LeftHand)
local rightHandCFrame = centerCFrame * VRService:GetUserCFrame(Enum.UserCFrame.RightHand)
9 Likes

is it alright if i use this in my game if not I will have to figure something else out thanks!

I think he posted it for people to use so I would assume it’s okay to use

1 Like

this doesnt work the controllers dont move with the camera when i turn around

Seems to work fine still.

Here’s a video tutorial I just did: https://www.youtube.com/watch?v=WIIAegsfJ98