2016-12-15 26 views
0

目前,我創建了一個對象列表,取決於是否在某個對象範圍內找到任何子對象。然後這個列表應該被傳遞給一個Function,它應該通過這個列表,縮小每個tile並逐個刪除它們。更改對象數組隨時間變化,然後逐個刪除它們

到目前爲止,這是我的代碼:

void OnMouseUp(){ 
    subobj = new GameObject[transform.childCount]; 
    RaycastHit hit; 
    hitCheck = true; 

    for (int i = 0; i < transform.childCount; i++) { 
     subobj [i] = transform.GetChild (i).gameObject; 
    }//store sub objects in array 

    foreach (GameObject child in subobj){ 
     if (Physics.Raycast (child.transform.position, Vector3.down, out hit)) { 
      if (hit.collider.tag != "Tile") 
       hitCheck = false; 
     } else { 
      hitCheck = false; 
     } 
     // if any one of the sub objects doesnt hit a tile then return to original position 
    } 

    if (hitCheck) { 
     foreach (GameObject child in subobj) { 
      Physics.Raycast (child.transform.position, Vector3.down, out hit); 
      child.transform.position = hit.transform.position; 
      child.transform.parent = hit.transform; 
      // if a object hit something, child each sub object to what it hit 

      GameObject.Find("Spawns").GetComponent<CreatingItems>().score += 1f; 
     } 
     transform.parent = null; 
     GameObject.Destroy (this.gameObject); 
     // destroy the old parent container 
    } else { 
     transform.position = startPoint; 
     transform.localScale = originalScale; 
     // return to original position 
    } 

    if (GameObject.Find ("Spawn1").transform.childCount == 0 && GameObject.Find ("Spawn2").transform.childCount == 0 && GameObject.Find ("Spawn3").transform.childCount == 0) { 
     CreatingItems ci = GameObject.Find ("Spawns").GetComponent<CreatingItems>(); 
     ci.Spawn(); 
    }// if spawn position is empty spawn new items 

    tileList = new GameObject[GameObject.Find ("Generated Map").transform.childCount]; 
    for (int i = 0; i < GameObject.Find ("Generated Map").transform.childCount; i++) { 
     tileList [i] = GameObject.Find ("Generated Map").transform.GetChild (i).gameObject; 
    }// stores all tiles as gameobjects in a list. 

    destroyList.Clear(); // clears the list of items to be destroyed 

    for (int i = 0; i < 9; i++) { 
     ChildCheckY = true; 
     ChildCheckX = true; 

     for (int y = 0; y <= 9; y++) { 
      if (tileList [(i*10) + y].transform.childCount == 0) { 
       ChildCheckY = false; 
       // checks if y values has children, if no child is found false is returned 
      } 
     } 
     for (int x = 0; x <= 9; x++) { 
      if (tileList[i + (10*x)].transform.childCount == 0) { 
       ChildCheckX = false; 
       // checks if x values has children, if no child is found false is returned 
      } 
     } 
     if (ChildCheckY) { 
      for (int y = 0; y <= 9; y++) { 
       destroyList.Add(tileList [(i * 10) + y].transform.GetChild (0).gameObject); 
       //Scale (tileList [(i * 10) + y].transform.GetChild (0).gameObject); 
       // destroys 10 y values in a row 
      } 
     } 
     if (ChildCheckX) { 
      for (int x = 0; x <= 9; x++) { 
       destroyList.Add(tileList [i + (10 * x)].transform.GetChild (0).gameObject); 
       //Scale (tileList [i + (10 * x)].transform.GetChild (0).gameObject); 
       // destroys 10 x values in a row 
      } 
     } 
    } 
    if(destroyList.Count>=10) 
     StartCoroutine (Scale (destroyList)); 
} 

IEnumerator Scale(List<GameObject> listdestroy){ 
    CreatingItems speed = GameObject.Find ("Spawns").GetComponent<CreatingItems>(); 
    foreach(GameObject gameobj in listdestroy){ 
     while (gameobj.transform.localScale.x > 0.2f){ 
      gameobj.transform.localScale -= new Vector3 (0.1f, 0.05f, 0.1f) * Time.deltaTime * speed.deletespeed; 
      yield return new WaitForSeconds (0.1f); 
     } 
    } 
    yield return null; 
} 

在刪除列表中的第一個對象得到由0.04降低,協程「縮放」後退出,並不會擴大項目的休息,當我更改代碼所有項目在1幀內縮放,而不是隨着時間的推移。 這是我的全部代碼

+0

你想要一個值每秒變化一次,比如說'10'。然後每次更新時檢查'Time.deltaTime'並將其值乘以它,這將以一個穩定的速率均勻地改變對象的大小。例如,如果幀之間的時間間隔爲0.2秒(上帝禁止),則它會將大小增加2.如果兩幀之間的時間間隔(更現實)爲0.02秒,則會增加0.2 –

回答

0

您的代碼:

IEnumerator destroyer(){ 
    CreatingItems speed = GameObject.Find ("Spawns").GetComponent<CreatingItems>(); 
    for (int i = 0; i < destroyList.Count; i++) { 
     do { 
      destroyList [i].transform.localScale = Vector3.MoveTowards(destroyList[i].transform.localScale, Vector3.zero,Time.deltaTime*speed.deletespeed); 
     } while (destroyList [i].transform.localScale.x > 0.1f); 
     Destroy (destroyList [i].gameObject); 
    } 
    yield return null; 
} 

是儘快刪除所有對象。你應該使用一些延遲內循環,如:

IEnumerator destroyer(){ 
    CreatingItems speed = GameObject.Find ("Spawns").GetComponent<CreatingItems>(); 
    for (int i = 0; i < destroyList.Count; i++) { 
     do { 
      destroyList [i].transform.localScale = Vector3.MoveTowards(destroyList[i].transform.localScale, Vector3.zero,Time.deltaTime*speed.deletespeed); 
      yield return new WaitForEndOfFrame(); // < == HERE! wait for end frame 
     } while (destroyList [i].transform.localScale.x > 0.1f); 
     Destroy (destroyList [i].gameObject); 
     yield return new WaitForEndOfFrame(); // < == HERE! wait for end frame 
    } 
    yield return null; 
} 
+0

,這幾乎可以奏效,現在第一個對象縮小了部分和停止,即時猜測什麼時候調用endofframe它退出co例程 – NanoedStux

+0

協程將以最後一個「yield return ...」語句結束。 –

+0

那爲什麼不是被破壞的物體呢? – NanoedStux

0

我想出了一個解決方案,我基本上wasnt允許協程,以移動到下一個對象,我打電話只OnMouseUp內前一次完成的,因此我改變下面的代碼來修復它

void Update(){ 
     StartCoroutine (Scale()); 
    } 
... 
IEnumerator Scale(){ 
    CreatingItems speed = GameObject.Find ("Spawns").GetComponent<CreatingItems>(); 
    foreach (GameObject gameobj in destroyList) { 
     yield return null; 
     while (Vector3.Distance (gameobj.transform.localScale, new Vector3 (0.1f, 0.05f, 0.1f)) > 0.1f) { 
      gameobj.transform.localScale -= new Vector3 (0.1f, 0.05f, 0.1f) * Time.deltaTime * speed.deletespeed; 
      yield return null; 
     } 
     Destroy (gameobj); 
    } 
} 
相關問題