只要在循环之外做你一次性的事情!
还有
randNum = Random.Range(0, 100);
然后你只使用 5 种不同的情况,并且只有当值更小 50 时(所以大约有一半的情况根本没有发生任何事情......)。如果这是有意的.. ok-ish .. 否则我宁愿使用列表和随机索引:
// HINT: Rather have a list for your prefabs
// this shrinks your code a lot
public List<GameObject/*or whatever type*/> eneymPrefabs = new List<GameObject>();
public Gamebject playerPrefab;
public GameObject endGamePrefab;
private void GenerateEnemies(int xMax, int zMax)
{
var landscape = new GameObject("ENEMIES");
// Do these only once
// store the references in case you need them later
var player = Instantiate(playerPrefab);
var endGame = Instantiate(endGamePrefab);
for (int z = 0; z < zMax; z++)
{
for (int x = 0; x < xMax; x++)
{
// simply pick a random index from the prefab list
int randIndex = Random.Range(0, eneymPrefabs.Count);
// and get the according random prefab
var enemyPrefab = enemyPrefabs[randIndex];
if(enemyPrefab) Instantiate(enemyPrefab, new Vector3(x * 10, 0, z * 10), Quaternion.identity /*, landscape.transform*/);
}
}
}
或者Draco18s提到的加权列表的例子
[Serializable]
public class WeightedPrefab
{
public GameObject Prefab;
public int Weight = 1;
}
public List<WeightedPrefab> weightedEnemyPrefabs;
public Gamebject playerPrefab;
public GameObject endGamePrefab;
private void GenerateEnemies(int xMax, int zMax)
{
// create a temp list using the weights and random index on this one
var enemyPrefabs = new List<GameObject>();
foreach(var item in weightedEnemyPrefabs)
{
for(var i = 0; i < item.Weight; i++)
{
enemyPrefabs.Add(item.Prefab);
}
}
// Rest stays the same
var landscape = new GameObject("ENEMIES");
// Do these only once
// store the references in case you need them later
var player = Instantiate(playerPrefab);
var endGame = Instantiate(endGamePrefab);
for (int z = 0; z < zMax; z++)
{
for (int x = 0; x < xMax; x++)
{
// simply pick a random index from the prefab list
int randIndex = Random.Range(0, eneymPrefabs.Count);
// and get the according random prefab
var enemyPrefab = enemyPrefabs[randIndex];
if(enemyPrefab) Instantiate(enemyPrefab, new Vector3(x * 10, 0, z * 10), Quaternion.identity /*, landscape.transform*/);
}
}
}
如果不是在每种情况下都实例化敌人是故意的,您仍然可以使用这两种方法并将预制参考留空 →对于该索引,不会实例化任何内容。
敌人和最终游戏应该是网格的一部分
在这种情况下,我会首先将整个网格组合写入一个列表。从这个列表中随机选择两个条目,然后将 player 和 endGame 放在那里。然后挡住这两个格子位置,不要在那里生成敌人:
[Serializable]
public class WeightedPrefab
{
public GameObject Prefab;
public int Weight = 1;
}
public List<WeightedPrefab> weightedEnemyPrefabs;
public Gamebject playerPrefab;
public GameObject endGamePrefab;
private void GenerateEnemies(int xMax, int zMax)
{
// Create a list of all awailable grid positions
var gridPositions = new List<Vector2Int>();
for (int z = 0; z < zMax; z++)
{
for (int x = 0; x < xMax; x++)
{
gridPositions.Add(new Vector2Int(x,z));
}
}
// pick the two random positions for player and endgame
var playerPosIndex = Random.Range(0, gridPositions.Count);
var playerPos = gridPositions[playerPosIndex];
gridPositions.RemoveAt(playerPosIndex);
var endGamePosIndex = Random.Range(0, gridPositions.Count);
var endGamePos = gridPositions[endGamePosIndex];
gridPositions.RemoveAt(endGamePosIndex);
// create a temp list using the weights and random index on this one
var enemyPrefabs = new List<GameObject>();
foreach(var item in weightedEnemyPrefabs)
{
for(var i = 0; i < item.Weight; i++)
{
enemyPrefabs.Add(item.Prefab);
}
}
var landscape = new GameObject("ENEMIES");
// Do these only once
// store the references in case you need them later
var player = Instantiate(playerPrefab, new Vector3(payerPos.x * 10, 0, playerPos.y * 10), Quaternion.identity /*, landscape.transform*/);
var endGame = Instantiate(endGamePrefab, new Vector3(endGamePos.x * 10, 0, endGamePos.y * 10), Quaternion.identity /*, landscape.transform*/);
for (int z = 0; z < zMax; z++)
{
for (int x = 0; x < xMax; x++)
{
// Now simply ignore the playerPos and endGamePos
if(x == playerPos.x && z == playerPos.y) continue;
if(x == endGamePos.x && z == endGamePos.y) continue;
// pick a random index from the prefab list
int randIndex = Random.Range(0, eneymPrefabs.Count);
// and get the according random prefab
var enemyPrefab = enemyPrefabs[randIndex];
// do nothing if enemyPrefab is null otherwise instantiate
if(enemyPrefab) Instantiate(enemyPrefab, new Vector3(x * 10, 0, z * 10), Quaternion.identity /*, landscape.transform*/);
}
}
}