c# - 如何将 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 轴。当我检查我的设备时也会发生同样的情况。当我使用手机中的工具检查了现实世界的北极在哪里,它在相反的方向。那么如何将我的模型指向北极。
解决方案
我会用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);
希望能帮助到你。
推荐阅读
- angular - 在高图中查找并填充折线图与绘图线的交点
- c++ - 检查 1/n 小数点后是否有无限位数
- rabbitmq - 消息卡在 RabbitMQ 上的 x-delayed-message Exchange 中
- mysql - 在 MYSQL 查询中求和时如何忽略 NULL?
- c# - 有没有办法使用 gecko c# 更改打印边距
- google-sheets - 使用非默认工作表在 BigQuery Cloud Shell 中从 Google 工作表创建外部表
- r - 绘制具有分类预测变量的支持向量机
- xamarin - Xamarin 表单 UWP 的 MasterDetail 页面中的标题问题
- chronicle-map - 创建一个堆外内存 ChronicleMap
- php - 使用 Symfony Serializer 将 JSON 反序列化为类实例数组