2017-09-09 68 views
0

這已經是一個問題了一個星期了。我似乎無法弄清楚。一旦顛倒,我試圖將我的潛艇翻轉過來。它在我的鼠標位置上旋轉。c#統一錯誤不能翻轉我的潛艇在其y軸

該代碼旋轉我的潛艇在鼠標位置,這是完美的。

Vector3 RayPos = cam.WorldToScreenPoint(transform.position); 
Vector3 dir = Input.mousePosition - RayPos; 
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; 
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward); 
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 
rotateSpeed * Time.deltaTime);[sub flipping y][1] 

現在這段代碼是我的問題。一旦我的sub顛倒它翻轉(y)但 它只會做一次,然後當我回去正確的方式它不會翻轉 (y)了。

Debug.Log(Mathf.Abs(Vector3.Dot(transform.up, Vector3.up))); 
if (Mathf.Abs(Vector3.Dot(transform.up, Vector3.down)) < 0.125f) 
{ 
    if (Mathf.Abs(Vector3.Dot(transform.right, Vector3.down)) > 0.825f) 
    { 
     SubTrans.localScale = new Vector3(transform.localScale.x, -1, 
     transform.localScale.z); 
    } 
    else 
    { 
     SubTrans.localScale = new Vector3(transform.localScale.x, 1, 
     transform.localScale.z); 
    } 
} 
+1

這是您第二次提出這個問題。你沒有得到答案,因爲它很難理解你的問題。也許你應該展示問題的動畫gif。 – Programmer

回答

0
if (Mathf.Abs(Vector3.Dot(transform.up, Vector3.down)) < 0.125f) 

我認爲這使你的代碼進行交換,只有當子是〜90度到地面。這很好,因爲絕對值意味着它出現在左側&右側。

if (Mathf.Abs(Vector3.Dot(transform.right, Vector3.down)) > 0.825f) 

然而,這是因爲絕對值的錯誤。它將&視爲相同(相對而言,因爲您使用的是transform.right),這意味着它總是翻轉你,因爲當前一個條件有效時abs(transform.right)總是> 0.8。

您應該檢查是否子被或向下朝上通過檢查這樣

if (Mathf.Abs(Vector3.Dot(transform.up, Vector3.up)) > 0f) //less than 90deg angle with up 
//flip sub upwards 
else 
//flip downwards 

向上的方向可以保留外,如果有條件的,如果你希望它在某些時候翻轉,但是這是所有需要的。