2016-04-19 10 views
0

SpawnScript(下限) SPAWN内に関数を作成したいので、インスペクタでオブジェクトを作成できる最小遅延時間と最大遅延時間を設定できます。オブジェクトを生成するまでの遅延時間を設定する

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

public class SpawnController : MonoBehaviour 
{ 

    public float maxWidth; 
    public float minWidth; 

    public float minTime; 
    public float maxTime; 

    public float rateSpawn; 
    private float currentRateSpawn; 

    public GameObject tubePrefab; 

    public int maxSpawnTubes; 

    public List<GameObject> tubes; 

    // Use this for initialization 
    void Start() 
    { 


     for (int i = 0; i < maxSpawnTubes; i++) { 
      GameObject tempTube = Instantiate (tubePrefab) as GameObject; 
      tubes.Add (tempTube); 
      tempTube.SetActive (false); 

     } 

     currentRateSpawn = rateSpawn; 

    } 

    // Update is called once per frame 
    void Update() 
    { 

     currentRateSpawn += Time.deltaTime; 
     if (currentRateSpawn > rateSpawn) { 
      currentRateSpawn = 0; 
      Spawn(); 
     } 
    } 

    private void Spawn() 
    { 

     float randWitdh = Random.Range (minWidth, maxWidth); 

     GameObject tempTube = null; 

     for (int i = 0; i < maxSpawnTubes; i++) { 
      if (tubes [i].activeSelf == false) { 
       tempTube = tubes [i]; 
       break; 
      } 
     } 

     if (tempTube != null) 
      tempTube.transform.position = new Vector3 (randWitdh, transform.position.y, transform.position.z); 
     tempTube.SetActive (true); 
    } 
} 
+0

あなたが行うすべてが使用され**起動します**。簡単にはできませんでした。ランダムな時間が必要な場合は、Random.Range(min、max)です。あなたは、あなたが言うように、公開変数としてmin、maxを設定することができます。 – Fattie

答えて

1

あなたはタイムスタンプのためTime.realtimeSinceStartupを使用することができます - これはちょっとあなたが気圧それを行う方法に合うでしょう。 、または非常に統一されたコルーチンを使用してください。 か、おそらくこれを実行する最短の方法であるinvokeを使用します。

http://docs.unity3d.com/ScriptReference/Time-realtimeSinceStartup.htmlhttp://docs.unity3d.com/Manual/Coroutines.html http://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html

編集: だけでなく、実際に、あなたもちょうどrateSpawn = Random.Range(minTime, maxTime);更新中のif文の内側、これはほとんどのあなたの現在のアプローチに合うことができました。

1

InvokeRepeating方法はコードの繰り返しに対処する方法です。そして毎に繰り返し repeatRate秒、

public void InvokeRepeating(string methodName, float time, float repeatRate); 

は時間(秒)メソッドmethodNameを呼び出します:繰り返し呼び出しとしてあなたは定義し、お好みに応じてスタートイベントの内側に繰り返し呼び出しを使用してスポーンメソッドを呼び出すと時刻を指定することができます。

この編集のようなものは、あなたのスクリプトで必要になります。

void Start(){ 
InvokeRepeating("Spawn",2, 0.3F); 
} 
関連する問題