'Equally spawning assets around terrain
I have a terrain I have generated and I am looking to equally distribute assets around this terrain so there is not more of some assets and not many of another. I am not sure how I could approach equally distributing 6 unique assets equally around the terrain.
I am not sure if calculating the percentage of an asset appearance by doing Unique asset/Total Assets is the right approach.
To spawn objects (spawning collectable coin in this case) I am currently generating a random position between 0 and the size of the terrain and randomly giving it a chance to spawn. The terrain has been generated with a Grid which allows me to place the objects at a valid node position so that a path finder can access it.
I am looking to have a set of 6 unique assets and distribute them equally around the terrain.
Quite new to C# and Unity and was wondering how I could approach this. Would appreciate any insight.
if(SpawnCoins)
{
int range = 3;
var x = Random.Range(0, GridData.Width);
var y = Random.Range(0, GridData.Height);
var NodePosition = GridData.grid[x,y];
if(NodePosition.terrainType == TerrainType.Grass || NodePosition.terrainType == TerrainType.Sand)
{
if(Random.Range(1, range) == 1)
{
if(!GridData.IsCellOccupied(NodePosition))
{
if(SpawnedCoins < SpawnCoinsAmount)
{
Vector3 spawnPosition = new Vector3(NodePosition.Position.x, NodePosition.Position.y, NodePosition.Position.z);
var coinAsset = Instantiate(Coin, spawnPosition, Quaternion.identity);
coinAsset.transform.SetParent(this.transform);
SpawnedCoins++;
} else {
SpawnCoins = false;
return;
}
}
range += 10;
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
