简体   繁体   English

在Unity中重新生成破坏的对象

[英]Respawn destroyed object in Unity

I have this script where as to when i hit a trigger my enemy spawns at a random time then the enemy destroy itself at a random time. 我有这个脚本,当我击中扳机时,敌人会随机产生,然后敌人会随机摧毁自己。 I want to respawn the enemy again so it can do this over and over again. 我想再次重生敌人,以便它可以一遍又一遍地做。 Any Suggestions: 有什么建议么:

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);
}
}

Hi Ghostdre this question would probably be better answered here Game Dev SO , as for your question I would recommend creating an Enemy class object and data members for the GameObject as well as a time variable which determines how long the Enemy should live before being Destroyed. 嗨,Ghostdre,您可以在Game Dev SO上更好地回答这个问题,对于您的问题,我建议为GameObject创建一个Enemy类对象和数据成员,以及一个时间变量,该变量确定被毁灭之前敌人应该生存多长时间。

eg 例如

public class SpawnManager
{
    public float lifeTime;

    ...

    void Update()
    {
        lifeTime -= Time.deltaTime

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

Please note that this is an incomplete example, but it should give you an idea of where to go from here. 请注意,这是一个不完整的示例,但是它应该使您对从这里出发的想法有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM