我不能在屏幕上有超过10个Enemy GameObjects

时间:2016-12-23 09:37:53

标签: c# unity3d instantiation

我有这段代码可以实例化敌人,但同时会遇到许多敌人。如何限制屏幕上允许的最大敌人数量?一旦他们开始产卵,那么屏幕上会有很多。

public class spn2 : MonoBehaviour {

    GameObject Enemy;
    //public GameObject EasyEnemey;
    public GameObject MediumEnemey;
    public GameObject HardEnemey;
    public Transform[] SpawnPoints;
    public float TimeBetweenSpawns;
    public int NumberOfEnemiesToSpawn;
    public int NumberOfMediumEnemiesToSpawn;
    public float EasyChance;
    public float MediumChance;
    public float HardChance;

    private int waveNumber;
    private float spawnTimer;
    private int numberOfEnemies;
    private int numberOfMediumEnemies;
    // Use this for initialization
    void Start()
    {
        //this below is the time to spawn so if 4 , every 4 seconds 1 will spawn  etc
        this.spawnTimer = 3.0f;
        this.waveNumber = 0;
        float totalChance = this.EasyChance + this.MediumChance + this.HardChance;
        if(Mathf.Abs(totalChance-1.0f)>0.0001f) { 
            Debug.LogWarning("Warning: The chances should add up to 1.0 ("+totalChance+" currently)");
        }
    }

    // Update is called once per frame
    void  Update()
    {
        this.spawnTimer -= Time.deltaTime;
        if(this.spawnTimer<=0.0f)
        {
            Transform spawnPoint = this.SpawnPoints[Random.Range(0, this.SpawnPoints.Length)];
            Vector2 spawnPos = spawnPoint.position;
            Quaternion spawnRot = spawnPoint.rotation;
            switch(this.waveNumber)
            {
            case 0:
                //Instantiate(EasyEnemey, spawnPos,spawnRot);
            Instantiate(Resources.Load(Enemy) as GameObject, spawnPos, spawnRot);

                this.numberOfEnemies++;
                if(this.numberOfEnemies>=this.NumberOfEnemiesToSpawn)
                {
                    this.waveNumber++;
                }
                break;
            case 1:
                Instantiate(MediumEnemey, spawnPos, spawnRot);
                this.numberOfMediumEnemies++;
                if (this.numberOfMediumEnemies >= this.NumberOfMediumEnemiesToSpawn)
                {
                    this.waveNumber++;
                }
                break;
            case 2:
                float randomFloat = Random.value;
                if(randomFloat<this.EasyChance)
                {
                    Instantiate(Enemy, spawnPos, spawnRot);
                }
                else if(randomFloat<this.EasyChance+this.MediumChance)
                {
                    Instantiate(MediumEnemey, spawnPos, spawnRot);
                }
                else
                {
                    Instantiate(HardEnemey, spawnPos, spawnRot);
                }
                break;
            }
            this.spawnTimer = this.TimeBetweenSpawns;

        Destroy (gameObject, .7f);
        }

    }

}

1 个答案:

答案 0 :(得分:1)

这样做的一种方法是制作一个变量来存储产生的敌人数量。

示例:

public int maxEnemies = 10;
private int numberOfEnemies = 0;

然后在循环中

if(this.spawnTimer<=0.0f && numberOfEnemies < maxEnemies) {...}

每当你产生一个敌人时:

enemyCount++;

每次敌人产生时都会产生:

enemyCount--;