2016-03-20 35 views
0

我正在使用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

您提到您正在嘗試查看手機是否處於垂直方向。

我不知道你要找究竟如何準確獲得,但如果僅僅是所有你想確定,請嘗試以下操作:

if (Screen.orientation == ScreenOrientation.LandscapeLeft) { 
    // screen is horizontal 
} 
+0

是不是我要找的屏幕的方向,但更多的是手機本身的方向。謝謝你的Screen.orientation順便說一句,它是有用的設置我的遊戲水平只:) –