簡體   English   中英

如何設置要生成的游戲對象數量?

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

這是我的代碼,我不知道如何處理它。 我是 c# 的初學者,這是針對我們的項目的。 該項目是關於一個氣球彈出器,我無法設置要生成的氣球數量。 我計划將生成的氣球數量設置為 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;
        }
    }
}

試試這個代碼:

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.
        }
    }
}

由於您在更新中執行此操作,因此您可以在 Instantiate 調用后添加一個計數器。

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

然后,將您的 if 語句嵌套在另一個 if 檢查計數器中。

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

就我個人而言,我會制作一個帶有延遲的協程方法,其中包含一個 for 循環,而不是在更新中執行此操作。

樣本:

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

調用方法:

StartCoroutine(SpawnObjects(10));

向類中添加一個變量以獲得最大數量。 public完成此操作,因此您可以在檢查器中將其設置為您想要的數量。

還添加一個計數器變量並將其設置為0 這是私有的,因為只有這個類需要訪問它。 請序列化此字段。

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

使用下一個檢查啟動Update() ,當它計算為真時從該方法返回。

if (spawned >= spawnAmountMax)
    return;

在實例化 GameObject 之后,增加變量:

spawned++;

調整后的代碼

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