到达某一点Unity后物体不会破坏

时间:2014-08-18 08:07:41

标签: unity3d instantiation spawning

好吧所以我正在创造一个无限的跑步者,而我所使用的技术是让玩家保持静态并移动并实例化平台对象。 为此,我创建了一个作为平台的对象的数组列表。 然后我将它们添加进去并产生它们。 我也沿着z轴平移它们,我想要在z轴上销毁低于0的对象,然后添加另一个替换对象。 问题是它不会翻译对象,除非我添加另一个脚本来执行此操作,即使我添加了另一个脚本进行翻译,它也不会销毁或添加。

我的代码如下。如果您无法理解我的问题,请问我会再次尝试解释。

我有两个脚本。

1)PlatformManager:这个应用于空的GameObject并包含ArrayList。

public class PlatformManager : MonoBehaviour
{
    [HideInInspector]
    public List<GameObject> platforms = new List<GameObject>(); // List of Platfroms.
    public GameObject[] prefab; // Allow user to add as many prefabs through the inspactor.

    public static float noOfPlatforms;  // a variable to hold how many platoforms.
    [HideInInspector]
    public static float objectPosition; // Z position of the game object.
    [HideInInspector]
    public static float objectScale;

        // Use this for initialization
        void Start ()
        {
        noOfPlatforms = 6.0f;
        objectPosition = 0.0f;
        objectScale = 0.0f;


        platforms.Add((GameObject)Instantiate(prefab[Random.Range(0,prefab.Length)], new Vector3(0,0,0) ,Quaternion.identity));
        //platforms.Add((GameObject)Instantiate(prefab[Random.Range(0, prefab.Length)], new Vector3(0, 0, 40.40114f), Quaternion.identity));
        for(int i = 0; i < noOfPlatforms; i++){
            objectScale = platforms[platforms.Count-1].transform.localScale.z;
            objectPosition = platforms[platforms.Count-1].transform.localPosition.z;

            platforms.Add((GameObject)Instantiate(prefab[Random.Range(0,prefab.Length)], new Vector3(0,0,(objectPosition + objectScale)+277f) ,Quaternion.identity));
        }
        }

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

        }
}

2)PlatformController:这个应该实例化并销毁和翻译对象。

public class PlatformController : MonoBehaviour {
    //private bool canBeDestroy = true; //  Flag to check whether the gameObject shoud be destroyed or not.
    [HideInInspector]
    public PlatformManager managePlateform; //  Reference to plateformManager script.

    [HideInInspector]
    public float plateformSpeed = 10f;
    [HideInInspector]
    public GameObject allPlatforms;
    [HideInInspector]


    //  Awake is called when the script instance is being loaded.
    void Awake()
    {
        //  Accessing the plateformManager script for the Plateform.
        managePlateform = GameObject.FindGameObjectWithTag("Platform").GetComponent<PlatformManager>();
    }

    void Start()
    {

    }


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

        //  Get the first platform object.
        GameObject firstPlatform = managePlateform.platforms[0];
        //  Get the last platform object.
        GameObject lastPlatform = managePlateform.platforms[managePlateform.platforms.Count - 1];

        if (firstPlatform.transform.localPosition.z < 0f)
        {
            Destroy(firstPlatform.gameObject); // destroy the first plateform gameObject.

            managePlateform.platforms.Remove(firstPlatform); // also remove the destroyed object from the list.

            // When the game object is destroyed then instantiate one gameobject into list and add at the last point.
            managePlateform.platforms.Add((GameObject)Instantiate(managePlateform.prefab[Random.Range(0, managePlateform.prefab.Length)], new Vector3(0, 0, (lastPlatform.transform.localPosition.z + lastPlatform.transform.localScale.z) + 277f), Quaternion.identity));
        }
        // Move the available platforms in the list along the z-axis
        foreach (GameObject platform in managePlateform.platforms)
        {
            platform.transform.Translate(0, 0, -plateformSpeed * Time.deltaTime);
        }
    }

}

0 个答案:

没有答案