簡體   English   中英

如何檢查設備是否已在Unity中的所有軸上旋轉

[英]How to check if device has been rotated on all axis in Unity

如果設備已在其所有軸上旋轉,我想檢查Unity。 所以,我正在閱讀所有軸的旋轉。

我應該怎么做才能驗證用戶是否已經在X軸上“翻轉”了他的設備? 我需要檢查該值,並看到它們在循環中包含0度,90度,180度和270度。

這是我的代碼的一部分:

void Update () {

    float X = Input.acceleration.x;
    float Y = Input.acceleration.y;
    float Z = Input.acceleration.z;
    xText.text =  ((Mathf.Atan2(Y, Z) * 180 / Mathf.PI)+180).ToString();
    yText.text = ((Mathf.Atan2(X, Z) * 180 / Mathf.PI)+180).ToString();
    zText.text = ((Mathf.Atan2(X, Y) * 180 / Mathf.PI)+180).ToString();

}

加速度計僅告知您設備的加速度是否發生變化。 因此,如果設備開始移動或停止移動,您將擁有值。 您無法從中檢索其方向。

相反,您需要使用設備的陀螺儀 現在大多數設備都有一個。

幸運的是,Unity通過陀螺儀課程支持陀螺

簡單地使用

Input.gyro.attitude

將以四元數的形式為您提供空間中設備的方向。

要檢查角度,請使用eulerAngles函數,例如,設備是否在x軸上翻轉:

Vector3 angles = Input.gyro.attitude.eulerAngles;
bool xFlipped = angles.x > 180;

注意,如果要在Unity中應用旋轉,則可能必須反轉某些值(因為它取決於設備使用哪個方向作為正值,向左或向右)

// The Gyroscope is right-handed.  Unity is left handed.
// Make the necessary change to the camera.
private static Quaternion GyroToUnity(Quaternion q)
{
    return new Quaternion(q.x, q.y, -q.z, -q.w);
}

以下是doc(Unity版本2017.3)的完整示例,以防上面的鏈接被破壞。 它顯示了如何從陀螺儀讀取值,並將它們應用於Unity中的對象。

// Create a cube with camera vector names on the faces.
// Allow the device to show named faces as it is oriented.

using UnityEngine;

public class ExampleScript : MonoBehaviour
{
    // Faces for 6 sides of the cube
    private GameObject[] quads = new GameObject[6];

    // Textures for each quad, should be +X, +Y etc
    // with appropriate colors, red, green, blue, etc
    public Texture[] labels;

    void Start()
    {
        // make camera solid colour and based at the origin
        GetComponent<Camera>().backgroundColor = new Color(49.0f / 255.0f, 77.0f / 255.0f, 121.0f / 255.0f);
        GetComponent<Camera>().transform.position = new Vector3(0, 0, 0);
        GetComponent<Camera>().clearFlags = CameraClearFlags.SolidColor;

        // create the six quads forming the sides of a cube
        GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);

        quads[0] = createQuad(quad, new Vector3(1,   0,   0), new Vector3(0,  90, 0), "plus x",
                new Color(0.90f, 0.10f, 0.10f, 1), labels[0]);
        quads[1] = createQuad(quad, new Vector3(0,   1,   0), new Vector3(-90,   0, 0), "plus y",
                new Color(0.10f, 0.90f, 0.10f, 1), labels[1]);
        quads[2] = createQuad(quad, new Vector3(0,   0,   1), new Vector3(0,   0, 0), "plus z",
                new Color(0.10f, 0.10f, 0.90f, 1), labels[2]);
        quads[3] = createQuad(quad, new Vector3(-1,   0,   0), new Vector3(0, -90, 0), "neg x",
                new Color(0.90f, 0.50f, 0.50f, 1), labels[3]);
        quads[4] = createQuad(quad, new Vector3(0,  -1,  0), new Vector3(90,   0,  0), "neg y",
                new Color(0.50f, 0.90f, 0.50f, 1), labels[4]);
        quads[5] = createQuad(quad, new Vector3(0,   0, -1), new Vector3(0, 180,  0), "neg z",
                new Color(0.50f, 0.50f, 0.90f, 1), labels[5]);

        GameObject.Destroy(quad);
    }

    // make a quad for one side of the cube
    GameObject createQuad(GameObject quad, Vector3 pos, Vector3 rot, string name, Color col, Texture t)
    {
        Quaternion quat = Quaternion.Euler(rot);
        GameObject GO = Instantiate(quad, pos, quat);
        GO.name = name;
        GO.GetComponent<Renderer>().material.color = col;
        GO.GetComponent<Renderer>().material.mainTexture = t;
        GO.transform.localScale += new Vector3(0.25f, 0.25f, 0.25f);
        return GO;
    }

    protected void Update()
    {
        GyroModifyCamera();
    }

    protected void OnGUI()
    {
        GUI.skin.label.fontSize = Screen.width / 40;

        GUILayout.Label("Orientation: " + Screen.orientation);
        GUILayout.Label("input.gyro.attitude: " + Input.gyro.attitude);
        GUILayout.Label("iphone width/font: " + Screen.width + " : " + GUI.skin.label.fontSize);
    }

    /********************************************/

    // The Gyroscope is right-handed.  Unity is left handed.
    // Make the necessary change to the camera.
    void GyroModifyCamera()
    {
        transform.rotation = GyroToUnity(Input.gyro.attitude);
    }

    private static Quaternion GyroToUnity(Quaternion q)
    {
        return new Quaternion(q.x, q.y, -q.z, -q.w);
    }
}

暫無
暫無

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

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