c#unity错误无法在y轴上翻转我的潜艇

时间:2017-09-09 04:31:22

标签: c# unity3d rotation flip

这已成为一个星期的问题。我似乎无法弄明白。我试图将我的潜艇在其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]   

现在这个代码是我的问题。一旦我的潜水员颠倒它就会翻转(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 个答案:

答案 0 :(得分:0)

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

我认为这会使你的代码仅在sub与地面相距约90度时执行交换。这很好,因为绝对值意味着它出现在左边和右边。右边。

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

然而,由于绝对值,这是错误的。它对待&amp;相同(因为你使用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

如果你想让它在特定时间翻转,你可以保留外部if条件,但这就是所需要的。