2014-09-25 565 views
0

我有一個問題,在它被銷燬後重新生成預製物。我似乎無法讓它在一秒之內被摧毀後恢復原來的起始位置。我創建了一個空的遊戲對象並將SpawnTargets.cs腳本附加到它。我不確定處理這種情況的最佳方法。另一個附有腳本的對象確實會破壞預製件。 BulletCollisionHandler.cs工作正常。謝謝你的幫助。代碼如下:Unity3D - 無法在物體被毀壞後重新生成物體

SpawnTargets.cs:

using UnityEngine; 
using System.Collections; 

public class SpawnTargets : MonoBehaviour 
{ 
    public GameObject targetCircle; 
    public GameObject targetSquare; 
    public GameObject targetStar; 

    private Vector3 circleSpawnPosition = new Vector3(0.0f, 1.227389f, -7.5f); 
    private Vector3 squareSpawnPosition = new Vector3(0.0f, 1.027975f, -7.993299f); 
    private Vector3 starSpawnPosition = new Vector3(0.0f, 1.8f, -7f); 

    // Use this for initialization 
    void Start() 
    { 

    } 

    // Update is called once per frame 
    void Update() 
    { 
     SpawnTarget(); 
    } 

    void SpawnTarget() 
    { 

    } 
} 

BulletCollisionHandler.cs:

using UnityEngine; 
using System.Collections; 

public class BulletCollisionHandler : MonoBehaviour 
{ 
    public GameObject targetCircle; 

    // Use this for initialization 
    void Start() 
    { 
     Destroy (gameObject, 2); 
    } 

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

    } 

    void OnCollisionEnter(Collision other) 
    { 
     if(other.gameObject.name == "TargetSquare") 
     { 
      other.gameObject.rigidbody.isKinematic = false; 
      ((TargetMovementHorizontal)other.gameObject.GetComponent<TargetMovementHorizontal>()).enabled = false; 

      Destroy (other.gameObject, 1); 
      Debug.Log("Hit square"); 
     } 
     else if(other.gameObject.name == "TargetCircle") 
     { 
      other.gameObject.rigidbody.isKinematic = false; 
      ((TargetMovementHorizontal)other.gameObject.GetComponent<TargetMovementHorizontal>()).enabled = false; 

      Destroy (other.gameObject, 1); 

      Debug.Log("Hit circle"); 
     } 
     else if(other.gameObject.name == "TargetStar") 
     { 
      other.gameObject.rigidbody.isKinematic = false; 
      ((TargetMovementHorizontal)other.gameObject.GetComponent<TargetMovementHorizontal>()).enabled = false; 
      ((TargetMovementVertical)other.gameObject.GetComponent<TargetMovementVertical>()).enabled = false; 

      Destroy (other.gameObject, 1); 
      Debug.Log("Hit star"); 
     } 
    } 
} 
+0

'SpawnTarget()'的內容是什麼? – 2014-09-25 02:53:35

回答

0

你不是叫實例化()的任何地方,所以很難看到新的對象將來自您提供的代碼。

在任何情況下,最好不要使用Destroy。如果您想立即重置對象,爲什麼不簡單地將其回收到開始位置?避免實例化和銷燬大量對象是一個好主意,最好隱藏/禁用不需要的對象,並取消隱藏/重新啓用它們。

這裏是a tutorial on the general idea.本教程是關於一組對象,但同樣的技巧也可用於回收單個對象。

0

你最好使用gameObject.SetActive(true/false);用於激活/取消激活遊戲對象,而不是僅使用Destroy。

然後,如果你使用的是摧毀你有3個選項,想到它在玩家看到它之前進入慾望位置。

1)禁用Renderer組件後啓用遊戲對象。然後你將變換的位置/旋轉平均爲你需要的位置/旋轉。之後,您重新啓用Renderer組件。它應該放在你想要的地方。

2)您實例化gameObject,但首先確保默認情況下Renderer組件在其Prefab上處於禁用狀態,以便您可以重新分配其轉換值,然後再次重新啓用Renderer。 3)你製作一個隱形的gameObject(一個空的gameObject)並且實例化想要的gameObject,然後讓Empty成爲新創建的gameObject的父代。假如父Empty正好在你想要的位置是的,當你實例化和重置孩子的位置時,它應該跳到空父母的頂部。

我沒有給出代碼,因爲你沒有,我也不知道你最終會喜歡哪種方法。在性能方面,啓用/禁用是最佳選擇。

正如theodox所說,Object Pooling是子彈等東西的最好朋友,儘管它可能會應用於許多其他遊戲對象,這些對象可能在遊戲邏輯上作爲「對象集合」工作。這完全值得學習。