0

I am spawning a bunch of assets randomly on my map and then spawning 4 random positions on the map which I will use to calculate the closest assets to those four random positions to "group" or "cluster" the assets. My goal is to have each group/cluster contain at least 1 of each asset. This is where I seem to be going wrong and I cannot seem to figure out how I could add an asset to a group/cluster by spawning it nearby the rest of the assets its grouped

Added the full spawning and clustering script to pastebin which may help build a better understanding.

SpawningAssetsAndClusteringScript.cs

Spawning all assets method

SpawnAssets = new List<GameObject>() { Tree, Coin, HealthPot, Enemy, Weapon, Trap1 };
//...
public void SpawnEveryAsset()
{

int TotalAmountToSpawn = 30;
int TotalAmountOfAssets = 6;
int TotalAmountToSpawnOfEach = TotalAmountToSpawn / TotalAmountOfAssets;

for(int i = 0; i < SpawnAssets.Count; i++)
{
    SpawnAnAsset(SpawnAssets[i], TotalAmountToSpawnOfEach);
}
}

// Instantiate the asset
void SpawnAnAsset(GameObject AssetToSpawn, int amount)
{
    for (int i = 0; i < amount; i++)
    {
        var pos = RandomPositionOnTerrain();
        var spawned = Instantiate(AssetToSpawn, pos, Quaternion.identity);
        SpawnedAssetsInLevel.Add(new Asset(spawned, pos));
    }
} 

Random position on terrain that is valid to spawn an asset

Vector3 RandomPositionOnTerrain()
{
    var x = UnityEngine.Random.Range(0, GridData.Width);
    var y = UnityEngine.Random.Range(0, GridData.Height);

    var NodePosition = GridData.grid[x, y];
    if (NodePosition.terrainType == TerrainType.Grass || NodePosition.terrainType == TerrainType.Sand)
    {
        if(!GridData.IsCellOccupied(NodePosition))
        {
            Vector3 LocationToSpawn = new Vector3(NodePosition.Position.x, (NodePosition.Position.y), NodePosition.Position.z);
            
            assetSpawnLocations.Add(LocationToSpawn);

            GridData.Obstacles.Add(NodePosition);

            return LocationToSpawn;
        } else
        {
            var pos = RandomPositionOnTerrain();
            var NodePos = GridData.grid[(int)pos.x, (int)pos.y];

            assetSpawnLocations.Add(pos);
            GridData.Obstacles.Add(NodePos);
            return pos;
        }
    } else
    {
        var pos = RandomPositionOnTerrain();
        var NodePos = GridData.grid[(int)pos.x, (int)pos.y];

        assetSpawnLocations.Add(pos);
        GridData.Obstacles.Add(NodePos);
        return pos;
    }
}

Example output: Tree and Trap are 0 in this group/cluster so I would want to spawn a tree and a trap nearby these other assets.

Cluster1:
TreeAmount: 0
HealthPotAmount: 1
EnemyAmount: 2
Trap1Amount: 0
CoinAmount: 1
Krellex
  • 613
  • 2
  • 7
  • 20
  • Should clusters "share" items? Otherwise it sounds easier to just find 4 random positions spread "far enough" away from each other and simply spawn items around it (`Random.insideUnitSphere`) .. then you could easily control minimum amount for each item and spawn the rest amount randomly – derHugo Mar 13 '22 at 06:26
  • @derHugo Thanks for getting back. Clusters should not share items which is why I want to spawn another asset near the cluster which is missing a particular asset so it will be within the range of the other items the cluster already clustered. This is is so the new spawn item would naturally fall into the cluster that is missing it. Hope that makes sense :) – Krellex Mar 13 '22 at 06:44

0 Answers0