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

时间:2018-06-26 12:21:23

标签: c# unity3d orientation

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

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

2 个答案:

答案 0 :(得分:0)

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

旋转对象

为此,您需要使用带有参数UnityARAlignment.UnityARAlignmentGravityAndHeading的ARKitSessionConfiguration对其进行配置 有关枚举的更多信息,请参见here

答案 1 :(得分:0)

我会用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);

希望有帮助。