我正在使用Unity制作手机游戏。它涉及使用陀螺仪来获取手机在太空中的方向/旋转。我一直很难比较C#中的两个四元数。这是我的代码:
transform.rotation = Input.gyro.attitude; // Take the gyroscope rotation data and copy it
transform.rotation.Set(0, 0, 0, 1); // change current Quaternion rotation data of the copy
// Copy shoud now represent a vertical rotation
Angle = Quaternion.Angle(Input.gyro.attitude,); // Calculate the Angle between the 2
if (Angle < 45) // if it's almost in vertical postion start the game
/* do something */
我实际上要做的是验证手机是否处于垂直位置(就像你将它贴在额头上一样)。我创建了原始方向的副本,并将其更改为我希望的方向。然后我比较两者,如果它们几乎相同,游戏可以继续。我对四元数几乎一无所知。我试图找出w,x,y,z的四元数值应该用于表示垂直方向但在谷歌搜索时找不到任何线索。所以这就是我在这里寻求帮助的原因。
答案 0 :(得分:0)
您提到您正在尝试查看手机是否处于垂直方向。
我不确定您想要获得的确切准确度,但如果只是您要确定的内容,请尝试以下操作:
if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
// screen is horizontal
}