c# - 在 Unity3D 上建模纬度和经度的困难
问题描述
我在用球体建模地球并使用另一个球体作为标记来标记地球球体的纬度和经度位置时遇到了一些麻烦。
我已将 x、y 和 z 比例设置为 635.6752,因为这是地球半径的 1000:1 比例(以米为单位)。我正在使用伦敦的经纬度定位。
这是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Earth : MonoBehaviour
{
public GameObject ObjectEarth;
public Transform ObjectMarker; // ObjectMarker object
public float radius = 635.6752f; // globe ball radius (unity units)
public float latitude = 51.5072f; // lat
public float longitude = 0.1275f; // long
// Start is called before the first frame update
void Start()
{
ObjectEarth.transform.localScale = new Vector3(radius, radius, radius);
latitude = Mathf.PI * latitude / 180;
longitude = Mathf.PI * longitude / 180;
// adjust position by radians
latitude -= 1.570795765134f; // subtract 90 degrees (in radians)
// and switch z and y (since z is forward)
float xPos = (radius) * Mathf.Sin(latitude) * Mathf.Cos(longitude);
float zPos = (radius) * Mathf.Sin(latitude) * Mathf.Sin(longitude);
float yPos = (radius) * Mathf.Cos(latitude);
// move ObjectMarker to position
ObjectMarker.position = new Vector3(xPos, yPos, zPos);
}
// Update is called once per frame
void Update()
{
}
}
这是场景视图中的输出:
我不太清楚为什么标记物体离地球物体表面这么远,理论上它应该在地球物体表面。
欢迎任何帮助和建议。
谢谢!
解决方案
Unity 的球体基元的基本半径为 0.5 个单位。因此,要制作球体的半径radius
,您需要将比例设置为该值的两倍:
ObjectEarth.transform.localScale = 2f * radius * Vector3.one;
推荐阅读
- c++ - 这个外部链接指令是什么意思?
- javascript - 有没有一种更简洁的方法可以在 Javascript 中对传单地图上的多个标记进行编码?
- python - PYTHON:plot_confusion_matrix() 缺少 2 个必需的位置参数:“X”和“y_true”
- c# - C# .Net - 在书签中插入文本而不打开 Word
- postgresql - Docker 容器 - 无法在浏览器上访问
- react-native - @gorhom 底页作为单独的组件
- javascript - TextEncoder 未定义。使用 nodejs 在 wsl2 上连接到 MongoDB
- php - 从 PHP74 更新到 PHP8 -> PDO::ATTR_TIMEOUT 不工作
- git - 不支持的 SSL 协议版本
- javascript - Cors 阻止了我对服务器的发布请求。请求的资源上不存在“Access-Control-Allow-Origin”标头。我尝试了所有解决方案