在Unity中重生被破坏的对象

时间:2014-12-16 02:16:33

标签: c# unity3d instantiation destroy

我有这个脚本,当我触发一个触发器时,我的敌人会在随机时间产生,然后敌人会在随机时间自行消灭。我想再次重生敌人,所以它可以一遍又一遍地做到这一点。任何建议:

public class SpawnManager : MonoBehaviour {

public GameObject Enemy; // the enemy prefab
public float mytimer; // the time to wait before spawn
public float transport;// the time it has to destroy itself

private GameObject _spawndEnemy; // the enemy that was spawnd

void SpawnEnemy()
{
    var enemySpawnPoint =  GameObject.Find("FFEnemySpawn1").transform;
    _spawndEnemy = Instantiate(
         Enemy, enemySpawnPoint.position, enemySpawnPoint.rotation) as GameObject;
    transport = Random.Range (2,15);
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.name == "FFTrigger") {
        mytimer = Random.Range(0,15);
        Invoke("SpawnEnemy", mytimer);
        Debug.Log("Spawn Normal");
    }
}

void Update()
{
    Destroy (_spawndEnemy, transport);
}
}

1 个答案:

答案 0 :(得分:0)

嗨Ghostdre这个问题可能会更好地回答Game Dev SO,至于你的问题,我建议为GameObject创建一个Enemy类对象和数据成员,以及一个确定如何的时间变量敌人应该在被摧毁之前生活。

e.g。

public class SpawnManager
{
    public float lifeTime;

    ...

    void Update()
    {
        lifeTime -= Time.deltaTime

        if (lifeTime <= 0)
        {
            Destroy (_spawndEnemy, transport);
            SpawnEnemy()
        }
    }
}

请注意,这是一个不完整的示例,但它应该让您知道从哪里开始。