首页 > 解决方案 > 如何将 ARkit 中的模型指向现实世界中的北极?

问题描述

嗨,我正在尝试一个始终在北方向的模型。如何在现实世界中获得北极方向。

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 轴。当我检查我的设备时也会发生同样的情况。当我使用手机中的工具检查了现实世界的北极在哪里,它在相反的方向。那么如何将我的模型指向北极初始位置

播放后

标签: c#unity3dorientation

解决方案


我会用Input.compass.magneticHeading. 您可以在此处查看官方文档以获取更多详细信息。

在检测到平面并放置 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);

希望能帮助到你。


推荐阅读