简体   繁体   English

如何在统一 2d 中创建随机生成器?

[英]How to create a random spawner in unity 2d?

I am making a game in Unity2D i which you have to match the correct projectiles with the enemies but I can't randomly spawn them, it just spawns one... (btw this is my first game)我正在 Unity2D 中制作游戏,您必须将正确的射弹与敌人匹配,但我不能随机生成它们,它只会生成一个……(顺便说一句,这是我的第一场游戏)

   {
       timeBTWSpawn = StartTimeBTWSpawn;

       private void Update()
   {

       if(timeBTWSpawn <= 0 )
       {
           rand = Random.Range(0, enemies.Length);
           Instantiate(enemies[0], SpawnPoint.transform.position, Quaternion.identity);```
           timeBTWSpawn = StartTimeBTWSpawn;
       }
       else
       {
           timeBTWSpawn -= Time.deltaTime;
       }
   }
}

i expect 3 different enemies to be randomly spawned but it only spawns the first one in the array.

there is one minor mistake.有一个小错误。 You are not actually using "rand" variable inside the instantiate method.您实际上并没有在实例化方法中使用“rand”变量。 having 0 in the index will always spawn the first element inside enemies array.it should be like this:索引中包含 0 将始终在敌人数组中生成第一个元素。它应该是这样的:

Code:代码:

       Instantiate(enemies[rand], SpawnPoint.transform.position, Quaternion.identity);

this will fix:)这将解决:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM