【发布时间】:2015-02-03 21:53:07
【问题描述】:
我正在制作一款太空游戏,您可以在其中射击来袭的小行星。这些小行星是使用 c# 脚本生成的,它允许我同时生成一定数量的敌人。 对于 2 个 int 数之间的 x 值,生成位置是随机的,但是在我当前的脚本中,一些敌人会相互生成,我无法弄清楚如何让每个敌人生成在不同的位置。 这是我的尝试,任何帮助将不胜感激。
public int spawnCount; //number of asteroids that are spawned
public Vector3 location;
public GameObject asteroid;
for (int i = 0; i < spawnCount; i++)
{
int locationGiver = 0;
int[] ChosenLocations = new int[];
//Create an array to store previous chosen locations
int RandomLocation = Random.Range (-6, 6);
for(int l = 0; l < ChosenLocations.Length ;l++)
{
//Check if RandomLocation is not the same as any previously determined locations
if(ChosenLocations[l] == RandomLocation)
{
RandomLocation = Random.Range (-6, 6);
l = 0;
//if it is the same, choose a new location and check if that one is different
}
}
location = new Vector3(RandomLocation, 0, 17);
//use the generated x value to create a location
Quaternion spawnRotation = Quaternion.identity;
//gives a random rotation to the asteroids, can be ignored
Instantiate (asteroid, location, spawnRotation);
//spawns the asteroid at the determined location
ChosenLocations[locationGiver] = RandomLocation;
locationGiver++;
//save the x-value in the array
}
【问题讨论】: