首页 > 解决方案 > 在 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()
    {

    }
}

这是场景视图中的输出:

Unity3D 编辑器场景视图中的代码输出

我不太清楚为什么标记物体离地球物体表面这么远,理论上它应该在地球物体表面。

欢迎任何帮助和建议。

谢谢!

标签: c#unity3dcoordinates

解决方案


Unity 的球体基元的基本半径为 0.5 个单位。因此,要制作球体的半径radius,您需要将比例设置为该值的两倍:

ObjectEarth.transform.localScale = 2f * radius * Vector3.one;

推荐阅读