如何在5秒后重新制作预制件?

时间:2014-08-09 22:00:09

标签: unity3d

我在破坏物体后,只有当物体被摧毁时才会产生一个预制件。

#pragma strict

function Update () {
    if (Input.GetMouseButtonDown(0)) {
        var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit)) {
           if (hit.collider.tag == "destroyable") {
               Destroy(hit.collider.gameObject);
            }
         }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以将Prefab放在一个变量中以在Unity编辑器上设置:

var objectToSpawn : GameObject;

在Spawn代码之前,您可以使用:

yield WaitForSeconds (5);

您的代码看起来像这样:

的Javascript

#pragma strict

var objectToSpawn : GameObject;

function Update () {
    if (Input.GetMouseButtonDown(0)) {
    var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit)) {
           if (hit.collider.tag == "destroyable") {
               var oldTransform = hit.collider.gameObject.transform;
               Destroy(hit.collider.gameObject);
               StartCoroutine(SpawnAfter5Seconds(oldTransform));
            }
         }
    }
}

function SpawnAfter5Seconds(oldTransform:Transform)
{
       yield WaitForSeconds (5);
       var newObject = Instantiate (objectToSpawn , oldTransform.position, oldTransform.rotation);
}