Unity-试图在Mapbox中的特定位置添加项目

问题描述 投票:0回答:2

我遵循了YouTube上的一个教程,在该教程中,开发人员在MapBox的图块上添加了以特定间隔在播放器周围生成的物品,且最大距离。我要达到的目的是将它们添加到特定的经度和纬度。

我所做的更改是要遍历我要添加的最大项,增加i变量。然后,我没有像newLat = getPlayerPosition() + random;那样在播放器周围生成该项目,而是对lat和lon进行了硬编码,例如:case 0: newLat = 48.30791f; newLon = 18.08611f; millenium_item.tag = "MI0"; break;

并且每个项目都会添加到项目列表中。我期望的行为是将每个项目添加到列表中,然后在地图上生成它们,但只添加了一个项目,该项目具有与玩家完全相同的纬度和经度:

case 3: newLat = 48.32232f; newLon = 18.09462f; millenium_item.tag = "MI3"; break;

案例3被添加,因为它与我的播放器(电话实际所在的位置)具有相同的纬度和经度。

case 4: newLat = 48.32232f; newLon = 18.09538f; millenium_item.tag = "MI4"; break;

案例4未添加,但是您可以看到它与案例3的位置非常接近。

我不知道我在做什么错。完整代码:

void SpawnItems() {

        itemindex = 100;
        for(int i = 0; i < count; i++)
        {
            if (googleSignIn.locationdata[i] == false)
            {
            MilleniumItemType type = (MilleniumItemType)(int)UnityEngine.Random.Range(0, Enum.GetValues(typeof(MilleniumItemType)).Length);
            float newLat = 0; 
            float newLon = 0;

            MilleniumPuzzle prefab = Resources.Load("MilleniumItems/puzzle", typeof(MilleniumPuzzle)) as MilleniumPuzzle;
            MilleniumPuzzle millenium_item = Instantiate(prefab, Vector3.zero, Quaternion.Euler(-100, 0, 0)) as MilleniumPuzzle;
            millenium_item.tileManager = tileManager;

                switch (i)
                {
                     ...
                case 3: newLat = 48.32232f; newLon = 18.09462f; millenium_item.tag = "MI3"; break;
                case 4: newLat = 48.32232f; newLon = 18.09538f; millenium_item.tag = "MI4"; break;
                case 5: newLat = 48.32310f; newLon = 18.09536f; millenium_item.tag = "MI5"; break;
                     ...
                }
                /// OUR LAT: 48.32232 OUR LON: 18.09462

                    millenium_item.Init(newLat, newLon);
                    items.Add(millenium_item);
        }
        }

然后初始化看起来像这样:

public void Init(float _lat, float _lon) {
    lat = _lat;
    lon = _lon;
    UpdatePosition ();
}

和:

public void UpdatePosition() {
    float x, y;
    Vector3 position = Vector3.zero;

    geodeticOffsetInv (tileManager.getLat * Mathf.Deg2Rad, tileManager.getLon * Mathf.Deg2Rad, lat * Mathf.Deg2Rad, lon * Mathf.Deg2Rad, out x, out y);

    if ((lat - tileManager.getLat) < 0 && (lon - tileManager.getLon) > 0 || (lat - tileManager.getLat) > 0 && (lon - tileManager.getLon) < 0) {
        position = new Vector3(x, .5f, y);
    }
    else {
        position = new Vector3 (-x, .5f, -y);
    } 

    position.x *= 0.300122f;
    position.z *= 0.123043f;

    Debug.Log("Position of inited millenium puzzle: " + position.x + " z: " + position.z);

    transform.position = position;
}
unity3d geolocation mapbox latitude-longitude
2个回答
0
投票

很难确切说明是什么原因造成的,但是此代码块很有可能使索引超出范围异常。

for(int i = 0; i < count; i++)
{
    if (googleSignIn.locationdata[i] == false)
    {

相反,您应该这样做:

for(int i = 0; i < googleSignIn.locationdata.length; i++)
{
    if (googleSignIn.locationdata[i] == false)
    {

但是,还有另一个问题。

此类和googleSignIn类都在Start()中进行了初始化,因此不能保证googleSignIn将首先执行。要解决此问题,您应该将SpawnItems();调用移至Awake()而不是Start()


0
投票

我能够自己解决问题。将项目添加到精确坐标永远无法正常工作,因此我将项目坐标保存在数组中。然后在update()中,我将播放器的位置与项目应位于的坐标进行比较。如果位置之间的差异是我定义的常数之间的差异,则这些项将出现在播放器周围,虽然不完全是在建筑物上,但是我不介意。示例:

   `public void SpawnItems() {

    itemindex = 100;
    for(int i = 0; i < count; i++)
    {

        if (googleSignIn.locationdata[i] == false && spawnedItem[i] == false)
        {
            if(tileManager.getLat - itemLats[i] < 0.003f && tileManager.getLat - itemLats[i] > -0.003f && tileManager.getLon - itemLons[i] < 0.00103f && tileManager.getLon - itemLons[i] > -0.00103f)
            {
                    MilleniumItemType type = (MilleniumItemType)(int)UnityEngine.Random.Range(0, Enum.GetValues(typeof(MilleniumItemType)).Length);


                    MilleniumPuzzle prefab = Resources.Load("MilleniumItems/puzzle", typeof(MilleniumPuzzle)) as MilleniumPuzzle;
                    MilleniumPuzzle millenium_item = Instantiate(prefab, Vector3.zero, Quaternion.Euler(-100, 0, 0)) as MilleniumPuzzle;
                    millenium_item.tileManager = tileManager;

                    float newLat = tileManager.getLat + UnityEngine.Random.Range(-0.0001f, 0.0001f);
                    float newLon = tileManager.getLon + UnityEngine.Random.Range(-0.0001f, 0.0001f);

                    if(newLat == previousLat || newLon == previousLon)
                {
                     newLat = tileManager.getLat + UnityEngine.Random.Range(-0.0001f, 0.0001f);
                     newLon = tileManager.getLon + UnityEngine.Random.Range(-0.0001f, 0.0001f);
                }

                previousLon = newLon;
                previousLat = newLat;

                    spawnedItem[i] = true;

                    millenium_item.tag = "MI" + i;

                    millenium_item.Init(newLat, newLon);
                    items.Add(millenium_item);  
            }
    }
    }
}`
© www.soinside.com 2019 - 2024. All rights reserved.