2016-12-23 6 views
0

私はこのコードを敵をインスタンス化していますが、多くの敵にはすぐにあります。画面上で同時に許可される敵の最大数を制限するにはどうすればよいですか?彼らが産卵を始めると、画面上に何トンもの卵が現れます。画面に10個以上の敵ゲームオブジェクトを持つことはできません

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); 
     } 

    } 

} 
+0

あなたが使用しているプログラミング言語の言語タグが含まれている場合は、より高速な助けを得るでしょう。 – Filburt

+0

こんにちは、ありがとう、私はそのコードを置くだろうコーディングでは良くありませんでしたか?私はそれがC#ユニート2Dの – matt

答えて

1

それを行うための1つの方法を、敵の量がで生成された格納する変数を作ることであろう

例:ループ内で、その後

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

if(this.spawnTimer<=0.0f && numberOfEnemies < maxEnemies) {...} 
毎回
enemyCount++; 

は毎回敵を生み出します:

enemyCount--; 
+0

であることを知らせてくれてありがとう?私はコード内の小さな間違いを知っている – matt

関連する問題