简体   繁体   English

如何设置要生成的游戏对象数量?

[英]how can I set an amount of game objects to spawn?

here is my code and i have no idea how to work it up.这是我的代码,我不知道如何处理它。 im a beginner in c# and this is for our project.我是 c# 的初学者,这是针对我们的项目的。 the project is about a balloon popper and i am having trouble to set an amount of balloons to spawn.该项目是关于一个气球弹出器,我无法设置要生成的气球数量。 I am planning to set the amount of balloons to spawn at 5, 10 even 20 and after that, the spawning will stop.我计划将生成的气球数量设置为 5、10 甚至 20,之后,生成将停止。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnScript: MonoBehaviour
{
    public Transform[] spawnPoints;
    public GameObject[] balloons;

    public float spawnTime = 0f;
    float spawnTimeLeft = 0f;

    // Start is called before the first frame update
    void Update()
    {
        if (spawnTimeLeft >= spawnTime)
        {
            int randBalloon = Random.Range(0, balloons.Length);
            int randSpawnPoint = Random.Range(0, spawnPoints.Length);

            Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
            spawnTimeLeft = 0f;
        }
        else
        {
            spawnTimeLeft = spawnTimeLeft + Time.deltaTime;
        }
    }
}

Try this code:试试这个代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnScript: MonoBehaviour
{
    public Transform[] spawnPoints;
    public GameObject[] balloons;

    public float spawnTime = 5.0f;
    public float maxSpawnTime = 20.0f;
    
    private void Start()
    {
        Invoke(nameof(Spawn), spawnTime); //This will start the spawning process after the initially set spawnTime, so first this will wait for 5 seconds.
    }
    
    private void Spawn()
    {
        int randBalloon = Random.Range(0, balloons.Length);
        int randSpawnPoint = Random.Range(0, spawnPoints.Length);

        Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
        
        spawnTime *= 2; //doubles the spawnTime, according to your request, so the second time this will be 10, and the third time, this will be 20 seconds
        if (spawnTime <= maxSpawnTime) //check if we reached or not the maximum spawn time
        {
            Invoke(nameof(Spawn), spawnTime); //if we not yet reached the maximum spawn time, it will start to wait and spawn one more balloon again.
        }
    }
}

since you're doing this in you update, you can add a counter after your Instantiate call.由于您在更新中执行此操作,因此您可以在 Instantiate 调用后添加一个计数器。

    Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
    counter++;

And then, nest your if statement inside another if checking for the counter.然后,将您的 if 语句嵌套在另一个 if 检查计数器中。

   if(counter < amountToSpawn)
   {
      //your code here
   }

For me personally, I would make a Coroutine method with delay that will have a for loop inside, rather than doing this in update.就我个人而言,我会制作一个带有延迟的协程方法,其中包含一个 for 循环,而不是在更新中执行此操作。

Sample:样本:

private IEnumerator SpawnObjects(int spawnCount)
{
   for(int i = 0; i < spawnCount; i++)
   {
      //Instantiate here
      yield return new WaitForSeconds(5);
   }
}

To call the method:调用方法:

StartCoroutine(SpawnObjects(10));

Add a variable to the class for the maximum amount.向类中添加一个变量以获得最大数量。 Have done this as public so you can set it in the inspector to the amount you wish.public完成此操作,因此您可以在检查器中将其设置为您想要的数量。

Also add a counter variable and set it to 0 .还添加一个计数器变量并将其设置为0 This is private as nothing but this class needs access to it.这是私有的,因为只有这个类需要访问它。 Do serialize this field.请序列化此字段。

public int spawnAmountMax = 5;
[SerializeField]
private int spawned = 0;

Start the Update() with the next check, return from the method when it evaluates to true.使用下一个检查启动Update() ,当它计算为真时从该方法返回。

if (spawned >= spawnAmountMax)
    return;

Than after instantiating the GameObject, increase the variable:在实例化 GameObject 之后,增加变量:

spawned++;

The adjusted code调整后的代码

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;

public class SpawnScript : MonoBehaviour
{

    public int spawnAmountMax = 5;
    private int spawned = 0;

    public Transform[] spawnPoints;
    public GameObject[] balloons;

    public float spawnTime = 0f;
    float spawnTimeLeft = 0f;

    // Start is called before the first frame update
    void Update()
    {

        if (spawned >= spawnAmountMax)
            return;

        if (spawnTimeLeft >= spawnTime)
        {
            int randBalloon = Random.Range(0, balloons.Length);
            int randSpawnPoint = Random.Range(0, spawnPoints.Length);

            Instantiate(balloons[randBalloon], spawnPoints[randSpawnPoint].position, transform.rotation);
            spawnTimeLeft = 0f;

            spawned++;

        } else
        {
            spawnTimeLeft = spawnTimeLeft + Time.deltaTime;
        }
    }
}

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

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