1
\$\begingroup\$

I currently generate the maptiles using Tags purely, which works great but doesnt satisfy my needs. I want to delete tiles that i dont need(since my player will only be going to the right) but I have a problem with checking when to delete old tiles so that i can generate new ones. My idea would be checking how close the player is to the last tile given and then delete the first, but i cant do that with tags since tags are given in the order that unity finds the GOs with that tag and not from left to right AFAIK. What would be the best way to get the last ground tile? (the one that got generated last).

using UnityEngine;
using System.Collections.Generic;

public class MapGenerator : MonoBehaviour

{
    private GameObject[] groundTiles;

/// <summary>
///  Represents the minimum amount of active tiles needed
/// </summary>
public int ActiveTiles;

void Start()
{
}

void Update()
{
    groundTiles = GameObject.FindGameObjectsWithTag("ground");

    if (groundTiles.Length < ActiveTiles)
        SpawnTile();
}

public void SpawnTile()
{
    GameObject tile = (GameObject)Instantiate(groundTiles[groundTiles.Length-1]);

    Vector3 tmpVec = tile.transform.position;
    tmpVec = tile.transform.position;
    tmpVec.x += 1;
    tile.transform.position = tmpVec;
}

public void DespawnTile(int tileIndex)
{
    List<GameObject> tmp = new List<GameObject>(groundTiles);
    tmp.RemoveAt(tileIndex);
    groundTiles = tmp.ToArray();
}
}
\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

Two main ways come to mind: Either you make the groundTiles array contain a fixed number of GameObjects, which you instantiate in the Start() method and then don't destroy, but move. So instead "Spawning" or "Despawning" objects, you are actually always reusing the same ones. (This is called pooling, if you would like to look it up.) Alternatively you can Spawn an object, add it to the groundTiles array and, when destroying GameObjects to despawn them, you remove either the first or last item of the array and shift all the items in the array over by one. (Whether you are destroying the first or last item depends on your implementation).

Pooling:

I'll only provide the pooling code (which will probably need some tweaking), as it is the correct way of doing this. Instantiating and destroying objects is fairly resource intensive and destroying in particular can be fairly unpredictable. Copying arrays is also very resource intensive. And while the code below can be improved in many ways, it can be a starting point for you.

public GameObject TilePrefab; //Prefab for the tile, assign in editor
int Tiles = 10; //Number of tiles to have at any one time (pool size)
float TileWidth = 10; //Width of tile, will need tweaking depending on your tile size.

void Start()
{
    groundTiles = new GameObject[tiles];
    for(int i = 0; i < tiles; i++){
        groupTiles[i] = instanteate(TilePrefab);
    }
}

void Update()
{
    for(int i = 0; i < tiles; i++){
        float dX =  groupTiles[i].transform.position.x - Player.transform.position.X;  //Get how far away the tile is from the player along the X axis
        if(dX < -40){    //Distance from the player at which tiles despawn.
            groupTiles[i].transform.Translate(Tiles * TileWdith, 0, 0); //Move the tile forward
        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

Add to groundTiles when you spawn a tile, instead of repopulating it every update. Then you can just use the array to find the last generated when you want to delete one.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .