1 year ago
#282334
Krellex
Spawning an asset nearby group of assets
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
c#
unity-game-engine
k-means
procedural-generation
0 Answers
Your Answer