Spawn gameObject horde,修改生成对象的浓度
Spawn gameObject horde, modify concentration of spawned objects
我将如何创建一个非均匀随机生成的游戏对象(敌人)来模拟 "horde" 的形成,如下图所示:
我希望前面有更多游戏对象,而随着它向后移动则更少。我想制作一个空游戏对象并让敌人使用如下代码作为目标:
public Vector3 target : Transform;
if (target == null && GameObject.FindWithTag("Empty"))
{
target = GameObject.FindWithTag("Empty").transform;
}
但是,这样做不会给我带来 "trail" 返回更少 int 的效果。
这是我随机生成敌人的代码,如果有帮助的话:
void SpawnHorde()
{
for (int i = 0; i < hordeCount; i++)
{
Vector3 spawnPosition = new Vector3(Random.Range (0, 200), 50, Random.Range (0, 200));
Instantiate(Resources.Load ("Prefabs/Sphere"), spawnPosition, Quaternion.identity);
}
}
有人对如何实现这一点有什么建议吗?
我执行@Jerry 代码后的结果:
更集中在前面;后面少了:)
我会听取 Maximilian Gerhardt 的建议。这是一些原始实现,您可以根据需要对其进行调整。最重要的调整是在一列中定位,你可以用一些随机数来实现。
void SpawnHorde()
{
int hordeCount = 200;
float xPosition = 0;
const int maxInColumn = 20;
while (hordeCount > 0)
{
int numberInColumn = Random.Range(5, maxInColumn);
hordeCount -= numberInColumn;
if (hordeCount < 0)
numberInColumn += hordeCount;
for (int i = 0; i < numberInColumn; i++)
{
Vector3 spawnPosition = new Vector3(xPosition, 50, Random.Range(0, 100));
Instantiate(Resources.Load("Prefabs/Sphere"), spawnPosition, Quaternion.identity);
}
xPosition += (float)maxInColumn * 2f / (float)hordeCount;
}
}
我将如何创建一个非均匀随机生成的游戏对象(敌人)来模拟 "horde" 的形成,如下图所示:
我希望前面有更多游戏对象,而随着它向后移动则更少。我想制作一个空游戏对象并让敌人使用如下代码作为目标:
public Vector3 target : Transform;
if (target == null && GameObject.FindWithTag("Empty"))
{
target = GameObject.FindWithTag("Empty").transform;
}
但是,这样做不会给我带来 "trail" 返回更少 int 的效果。
这是我随机生成敌人的代码,如果有帮助的话:
void SpawnHorde()
{
for (int i = 0; i < hordeCount; i++)
{
Vector3 spawnPosition = new Vector3(Random.Range (0, 200), 50, Random.Range (0, 200));
Instantiate(Resources.Load ("Prefabs/Sphere"), spawnPosition, Quaternion.identity);
}
}
有人对如何实现这一点有什么建议吗?
我执行@Jerry 代码后的结果:
更集中在前面;后面少了:)
我会听取 Maximilian Gerhardt 的建议。这是一些原始实现,您可以根据需要对其进行调整。最重要的调整是在一列中定位,你可以用一些随机数来实现。
void SpawnHorde()
{
int hordeCount = 200;
float xPosition = 0;
const int maxInColumn = 20;
while (hordeCount > 0)
{
int numberInColumn = Random.Range(5, maxInColumn);
hordeCount -= numberInColumn;
if (hordeCount < 0)
numberInColumn += hordeCount;
for (int i = 0; i < numberInColumn; i++)
{
Vector3 spawnPosition = new Vector3(xPosition, 50, Random.Range(0, 100));
Instantiate(Resources.Load("Prefabs/Sphere"), spawnPosition, Quaternion.identity);
}
xPosition += (float)maxInColumn * 2f / (float)hordeCount;
}
}