簡體   English   中英

如何正確夾緊相機旋轉軸?

[英]How to clamp camera rotation axis correctly?

我試圖夾緊我的相機的 X 和 Y 軸,我已經設法做到了。 但是,當鉗位值達到 MAX 閾值時,它會跳回 MIN 閾值嗎? 知道是什么導致我的代碼出現這種情況嗎?

 private void ClimbingLookRotation()
    {
        if (input.mouseX != 0 || input.mouseY != 0f)
        {
            orientation.rotation *= Quaternion.AngleAxis(input.mouseX, Vector3.up);
            orientation.rotation *= Quaternion.AngleAxis(input.mouseY, Vector3.right);
        }

        var rotX = orientation.eulerAngles.x;
        var rotY = orientation.eulerAngles.y;

        rotX = Mathf.Clamp(rotX, 1, 25);
        rotY = Mathf.Clamp(rotY, 200, 355);            

        orientation.eulerAngles = new Vector3(rotX, rotY, 0);
    }

任何幫助,將不勝感激。 謝謝你。

您可以圍繞中心值重新定位歐拉角,然后鉗制差異:

float centralX = 13f;
float extentX = 12f;

float centralY = 277.5f;
float extentY = 77.5f;

private static float ClampEuler(float val, float center, float extent)
{
    return center + Mathf.Clamp((val - center + 360f) % 360f, -extent, extent);
}

private void ClimbingLookRotation()
{
    if (input.mouseX != 0 || input.mouseY != 0f)
    {
        orientation.rotation *= Quaternion.AngleAxis(input.mouseX, Vector3.up);
        orientation.rotation *= Quaternion.AngleAxis(input.mouseY, Vector3.right);
    }

    var rotX = orientation.eulerAngles.x;
    var rotY = orientation.eulerAngles.y;

    rotX = ClampEuler(rotX, centralX, extentX);
    rotY = ClampEuler(rotY, centralY, extentY);

    orientation.eulerAngles = new Vector3(rotX, rotY, 0);
}

順便說一句,您可能想做orientation.rotation *= Quaternion.AngleAxis(input.mouseY, orientation.right); 圍繞局部右側而不是全局右側旋轉。 隨着時間的推移,圍繞全局權利旋轉可能會產生您可能不希望的滾動效果。

我發現了一些效果很好的東西,所以我想我會回答,以防它對任何人有幫助!

 public static float ClampAngle(float angle, float min, float max)
    {   //Normalises angle value passed in between -180 to 180 to make the angle clampable
        angle = NormalizeAngle(angle);
        if (angle > 180)
        {
            angle -= 360;
        }
        else if (angle < -180)
        {
            angle += 360;
        }

        min = NormalizeAngle(min);
        if (min > 180)
        {
            min -= 360;
        }
        else if (min < -180)
        {
            min += 360;
        }

        max = NormalizeAngle(max);
        if (max > 180)
        {
            max -= 360;
        }
        else if (max < -180)
        {
            max += 360;
        }

        return Mathf.Clamp(angle, min, max);
    }

    public static float NormalizeAngle(float angle)
    { //If the angle is above or below 360 degrees, normalise it
        while (angle > 360)
            angle -= 360;
        while (angle < 0)
            angle += 360;
        return angle;
    }

然后只需在需要鉗位值的地方調用 ClampAngle 方法,例如:

private void ClimbingLookRotation()
    {
        if (input.mouseX != 0 || input.mouseY != 0f)
        {
            orientation.rotation *= Quaternion.AngleAxis(input.mouseX, Vector3.up);
            orientation.rotation *= Quaternion.AngleAxis(input.mouseY, Vector3.right);
        }

        var rotX = orientation.eulerAngles.x;
        var rotY = orientation.eulerAngles.y;

        rotX = HelperFunctions.ClampAngle(rotX, -10, 25); //Here
        rotY = HelperFunctions.ClampAngle(rotY, 200, 340); //And here

        orientation.eulerAngles = new Vector3(rotX, rotY, 0);
    }

我在我的相機旋轉中調用了它 function 來夾住 rotX 和 rotY,它后來應用於我的定向游戲 object 的旋轉。

再次感謝 Ruzihm 之前的幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM