Search code examples
unity-game-enginegeolocationmapboxlatitude-longitude

Unity - trying to add items at specific locations in Mapbox


I´ve followed a tutorial on YouTube where the developer added items spawning around the player at a certain interval with a max distance on the tile in MapBox. What I am trying to achieve is to add them at specific longitude and latitudes.

The changes I made was to to loop through the max amount of items I want to add, incrementing an i variable. Then instead of spawning the item around the player like newLat = getPlayerPosition() + random; I hardcoded the lat and lon like: case 0: newLat = 48.30791f; newLon = 18.08611f; millenium_item.tag = "MI0"; break;

And each item is added to a list of items. The behaviour I am expecting is to add each item to the list and then spawn them on the map, but instead only one item gets added, the one that has the exact same latitude and longitude as the player:

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

Case 3 gets added as it has the same Lat and Lon as my player (where the phone actually is).

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

Case 4 doesnt get added, but as you can see it is pretty close to the location of Case 3.

I dont know what I am doing wrong. Full code:

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);
        }
        }

Then Init looks like this:

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

and:

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;
}

Solution

  • I was able to solve the issue myself. Adding the items at exact coordinates never worked, so instead I am saving the item coordinates in an array. Then in update() I am comparing the position of the player with the coordinates where the items should be. If the difference between the positions are between constant numbers that I defined, then the items will appear around the player, although not exactly at the building, but in my case I don´t mind. Example:

       `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);  
                }
        }
        }
    }`