Reputation: 73
How would I create a non-uniform random generation of gameObjects (enemies) that mimic the formation of a "horde" like this image:
I want there to be more gameObjects in the front and less as it trails off towards the back. I thought of making an Empty gameObject and having the enemies target with code like this:
public Vector3 target : Transform;
if (target == null && GameObject.FindWithTag("Empty"))
{
target = GameObject.FindWithTag("Empty").transform;
}
However, doing that would not give me the "trail" effect where there are fewer int he back.
Here is my code for randomly generating enemies, if it helps:
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);
}
}
Does anyone have a suggestion as to how to achieve this?
My results after implementing @Jerry's code:
More concentrated in the front; less in the back :)
Upvotes: 0
Views: 213
Reputation: 2720
I would go for Maximilian Gerhardt suggestions. Here is some raw implementation, for you to tweak it as you want. The most important to tweak is positioning in one column, what you can achieve with some random numbers.
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;
}
}
Upvotes: 1