我在用一个球体对地球建模并使用另一个球体作为标记来标记地球球体的经度和纬度时遇到麻烦。
我已将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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句