代码之家  ›  专栏  ›  技术社区  ›  zyonneo

在现实世界中,如何将ARkit中的模型指向北极?

  •  1
  • zyonneo  · 技术社区  · 7 年前

    嗨,我试着做一个北向的模型。如何在现实世界中得到北极的方向。

    public class PointNoeth : MonoBehaviour {
    
    // Use this for initialization
    //void Start () 
    IEnumerator Start()
    {
        // First, check if user has location service enabled
        if (!Input.location.isEnabledByUser)
            yield break;
    
        // Start service before querying location
        Input.location.Start();
    
        // Wait until service initializes
        int maxWait = 20;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            yield return new WaitForSeconds(1);
            maxWait--;
        }
    
        // Service didn't initialize in 20 seconds
        if (maxWait < 1)
        {
            print("Timed out");
            yield break;
        }
    
        // Connection has failed
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            print("Unable to determine device location");
            yield break;
        }
        else
        {
            // Access granted and location value could be retrieved
            print("Location: " + Input.location.lastData.latitude + " " + Input.location.lastData.longitude + " " + Input.location.lastData.altitude + " " + Input.location.lastData.horizontalAccuracy + " " + Input.location.lastData.timestamp);
        }
    
        // Stop service if there is no need to query location updates continuously
        Input.location.Stop();
    
        Input.compass.enabled = true;
    }
    
    // Update is called once per frame
    void Update () 
    {
    
        transform.rotation = Quaternion.Euler(0, -Input.compass.trueHeading, 0);
    
        /
    }
    }
    

    在“更新”功能中,我设置了旋转。我将箭头标记保留为模型。最初,我将其指向z方向,但当我单击“播放”时,它将更改为-x轴。当我签入设备时,也会发生同样的情况。当我使用手机中的工具检查现实世界中的北极时,它指向的方向正好相反。因此,如何将模型指向北极 Initial position .

    After Playing

    1 回复  |  直到 7 年前
        1
  •  0
  •   Frothiny    7 年前

    可以将世界对齐方式更改为基于指南针方向,然后在放置对象时,可以根据 AR Kit World Alignment

    为此,需要使用参数UnityARAlignment.UnityARAlignmentGravityAndHeading的ARKitSessionConfiguration对其进行配置 here

        2
  •  0
  •   Digvijaysinh Gohil    6 年前

    我会用 Input.compass.magneticHeading . here 更多细节。

    在探测到平面并放置ar物体后,我这样旋转它

    arObject.transform.rotation = Quaternion.Euler(0f, -Input.compass.magneticHeading, 0f);
    


    如果我想面对南方,我会做些

    private float directionOffset = 180f;// 0f - north, 45f - northeast, 90f - east, 135f - southeast etc.
    arObject.transform.rotation = Quaternion.Euler(0f, -Input.compass.magneticHeading + directionOffset, 0f);
    

    希望有帮助。