So I am currently trying to make a VR character. I want it work so that when the player turns their head they will be able to rotate their head 80 degrees left or right and if they turn past that then the whole character will rotate with them.
This works fine right now, but only if the player is looking forward. If the player looks down at their feet or up at the sky then this stops working. This is because when they rotate their head the local Y axis of the headset changes and its not lined up with the global Y axis anymore.
Right now I am determining the rotation of the headset by taking the global CFrame of the headset and converting it to object space relative to the CFrame of the HumanoidRootPart. This way I can see how much it has been rotated away from HumanoidRootPart.
Does anyone know how I can find how much the headset is being rotated about the global Y-axis, relative to the HumanoidRootPart?
I can’t verify, as I don’t have a VR headset to test with, but you should be able to use CFrame:toOrientation() to extract the yaw, pitch, and roll of the headset’s CFrame.
I believe the order they are returned in are pitch, roll, yaw, but it’s been a while since I worked with CFrames.
It has to do with the order the rotations are applied, I don’t fully understand the mathematics behind it myself.
local x, y, z = cf:ToEulerAnglesXYZ()
-- assuming only rotation:
-- cf == CFrame.new()*CFrame.Angles(0,0,z)*CFrame.Angles(0,y,0)*CFrame.Angles(x,0,0)
y, x, z = cf:ToEulerAnglesYXZ() -- aka toOrientation
-- cf == CFrame.new()*CFrame.Angles(0,0,z)*CFrame.Angles(x,0,0)*CFrame.Angles(0,y,0)