2017-02-16 63 views
3

我試圖使拖動對象成爲可能。這個對象只能旋轉這麼多。 (Similair到門)。統一在最小和最大距離之間旋轉

這裏是代碼高級編輯,旋轉工作的對象。 我有2個向量maxrotation和minrotation。

只要用戶拖動可交互對象,就會調用此代碼。 (像更新,但只有拖動時)

 if (GestureManager.Instance.IsNavigating && 
    HandsManager.Instance.FocusedGameObject == gameObject) 
     { 
      //speed and navigiation of rotation 
      float rotationFactor; 
      rotationFactor = ManipulationManager.Instance.ManipulationPosition.y * RotationSensitivity; 
      totransform.Rotate(new Vector3(rotationFactor, 0, 0)); 
     } 

這將是偉大的,如果我可以在這裏使用if語句。我嘗試了很多東西,但仍然無法使用。

如上所述代碼粘貼在這裏工作。該對象應該是可拖動的,但只能達到某個點。

totransform是變換將旋轉

任何想法將是巨大的和最欣賞的。

親切的問候。

+0

嗯,你做了什麼?你可以在totransform.Rotate之前做一個'Debug.Log(rotationFactor);'在你想要的最大和最小位置上覆制值,然後做一個if(withinThatRange){Rotate}'。應該管用!或者說,首先檢查旋轉。 – Maakep

+0

btw「totransform」不是一個錯字,對吧? – Maakep

+0

啊對不起,我沒有詳細說明我會改變它。 totransform是將被轉換的對象的轉換。 –

回答

2

我想你想看看eulerAngles。檢查你得到的值,然後在做旋轉之前設置if語句。這是你找到你想要的值的示例代碼:

if (GestureManager.Instance.IsNavigating && 
    HandsManager.Instance.FocusedGameObject == gameObject) 
{ 
    //speed and navigiation of rotation 
    float rotationFactor = ManipulationManager.Instance.ManipulationPosition.y * RotationSensitivity; 

    Debug.Log(totransform.eulerAngles); 
    if (totransform.eulerAngles.x < 100) { 
     totransform.Rotate(new Vector3(rotationFactor, 0, 0)); 
    } 
} 
+0

所以我們假設在這種情況下,x = -90的最小旋轉和最大值爲0.當調試時,我檢查eulerangle中的X的值,它被轉換爲像3那樣的數字。原來的x旋轉是-80。但只有在運行應用程序後,它調用eulerangle –

+0

時它變爲3啊,在檢查員的權利... -1是359度。對於正數,它更有意義:http://imgur.com/a/l1qfC hehe。 – Maakep

+0

那麼,eulers會給你正確的角度,但它不會與檢查員中的數字相同。例如,逆時針-90度是順時針270度,這就是你從eulerangles得到的。使用transform.localEulerAngles。 – Maakep

0

所以這裏是解決方案,爲我工作。首先我聲明移動變量(在下面看不到,在這種情況下是2)。然後我追蹤所覆蓋的距離,並對此加以限制。

當然,這個代碼有一些改進,比如使用移動而不是2.但由於時間限制,我沒有這樣做。

if (GestureManager.Instance.IsNavigating && 
    HandsManager.Instance.FocusedGameObject == gameObject) 
     { 

       //here we get the movement direction and set it in movement. 
       if (GestureManager.Instance.NavigationPosition.y > 0) 
       { 
        movement = 2; 
       } 
       else if (GestureManager.Instance.NavigationPosition.y < 0) 
       { 
        movement = -2; 
       } 


      //the first part is false if we reach higher then maxdistance and the movement is going up 
      //the second part is false if we reach the lower distance and the movement is going down. 
      if ((!(distance > maxdistance.x) || movement < 0) && ((!(distance < mindistance.x) || movement > 0))) 
       { 
        //here we add the movement to the distance so we know if it gets closer or further 
        distance += movement; 
        //here we rotate 
        totransform.Rotate(new Vector3(movement, 0, 0)); 
       } 
     }