【问题标题】:Spawning a number of enemies in random but different locations在随机但不同的位置产生大量敌人
【发布时间】: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
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    首先,您需要指定 ChosenLocations 数组的大小。如所写,初始化数组的行将无法编译。如果大小是动态的,那么您需要使用其他类型,例如列表。

    为了解决问题,假设您的数组大小为 10。

    试试这样的:

    int[] ChosenLocations = new int[10];
    bool match;
    Random r = new Random(); //I am using the .NET random generator, since I don't have Unity3d
    for (int l = 0; l < ChosenLocations.Length; l++)
    {
        match = true;
        while (match)
        {
            int RandomLocation = r.Next(-6, 7);    
            if (!ChosenLocations.Contains(RandomLocation))
            {
                match = false;
                ChosenLocations[l] = RandomLocation;
                // Instantiate your asteroids here...
                Console.WriteLine(String.Format("Location {0}: {1}", l, ChosenLocations[l]));
             }
       }
    }
    Console.ReadKey();
    

    【讨论】:

    • 嘿,我用你的代码替换了我的代码,但现在 unity 在第一次生成时完全冻结了。我保留了所有内容(除了 .net 随机生成器,因为我使用的是统一)并添加了生成对象的代码。
    • 我对团结一无所知。我只是想解决您如何选择不重复的随机数的逻辑问题。你可能需要问另一个关于团结的问题。但是,如果您仔细查看我的代码,您会注意到生成部分要么需要发生在 while 循环内,要么发生在循环遍历 ChosenLocations 数组的每个单元的单独 foreach 循环中。我没有重组你的代码的产生部分;这取决于你。尝试调试您的代码以查看生成崩溃时究竟发生了什么。
    • 另一种可能性(因为您说统一冻结)是您陷入了无休止的 while 循环。如果您的最大小行星数量大于可能位置的数量,就会发生这种情况。
    猜你喜欢
    • 2011-10-26
    • 2020-06-11
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 2016-12-16
    相关资源
    最近更新 更多