【发布时间】:2022-11-19 17:37:17
【问题描述】:
我正在制作一款 2D 无尽跳跃游戏,您可以在其中跳上一个平台,它可能会有一个敌人。我希望有机会在那个平台上有两个敌人,而不是像在其他平台上那样只有一个。我还希望平台上有零敌人的机会。我也有一个名为“enemyCount”的浮点数,它在 0 到 2 之间是随机的,但我真的不知道如何实现它。我将在下面提供代码。我试着给“enemyCount”它自己的循环,但这使得它在开始时每个级别只产生 0 到 2 个敌人,因为循环是开始的。当然,将其放入更新会每秒产生敌人。我显然不会要求任何人为我编写代码。我真的不知道从哪里开始。
public GameObject Ground;
public int groundCount;
public GameObject Enemy;
int enemyCount;
// Start is called before the first frame update
void Start()
{
enemyCount = Random.Range(0, 2);
groundCount = Random.Range(100, 300);
for (int i = 0; i < groundCount; i++)
{
Vector2 EnemyPosition = new Vector2(Random.Range(-10f, 12f), i * 11);
Vector2 spawnPosition = new Vector2(0, i * 10);
Instantiate(Ground, spawnPosition, Quaternion.identity);
for(int x = 0; x < enemyCount; x++)
{
Instantiate(Enemy, EnemyPosition, Quaternion.identity);
}
}
}
【问题讨论】: