简体   繁体   English

统一摧毁后如何重生坦克? (有几秒钟的延迟)

[英]How to respawn the tank after being destroyed in unity? (with a few seconds delay)

The function I have achieved so far is that two tanks can shoot at each other and destroy each other, and cannot respawn after being destroyed.我目前实现的function是两辆坦克可以互相射击,互相摧毁,被摧毁后无法重生。 I tried to instantiate tank again after destroying it, but when I did this, the instantiate object lost all its properties.(It no longer becomes controllable or destroyed) My goal is that when the bullet hits the opponent's tank, it will destroy the opponent.我试图在摧毁它后再次实例化坦克,但是当我这样做时,实例化的object失去了它的所有属性。(它不再变得可控或破坏)我的目标是当子弹击中对手的坦克时,它会摧毁对手. The opponent will immediately disappear and resurrect in the default position (or any position) after 4 seconds delay.对方会在4秒延迟后立即消失并在默认的position(或任意位置)中复活。

Here is my code:这是我的代码:

public class Bullet: MonoBehaviour {公共 class 项目符号:MonoBehaviour {

GameObject tank1;
GameObject tank2;
public int bullet;


// Start is called before the first frame update
void Awake()
{
    tank1 = GameObject.Find("Tank1");
    tank2 = GameObject.Find("Tank2");

}


void OnTriggerEnter2D(Collider2D other)
{   
    //If the bullet from tank 1 hits the object
    if (bullet == 1)
    {
        if (other.tag == "Wall")
        {
            Destroy(this.gameObject);
        }

        if (other.tag == "Tank2")
        {
            Destroy(this.gameObject);
            Destroy(other.gameObject);
            //tank2 = Instantiate(tank2, transform.position, transform.rotation);

        }
    }
    
   //If the bullet from tank 2 hits the object
    else if (bullet == 2)
    {
        if (other.tag == "Wall")
        {
            Destroy(this.gameObject);
        }

        if (other.tag == "Tank1")
        {
            Destroy(this.gameObject);
            Destroy(other.gameObject);
            //tank1 = Instantiate(tank1, transform.position, transform.rotation);
        }
    }
}

private void Respawn()
{

}

} }

Don't destroy the tank.不要破坏坦克。 Turn it off.把它关掉。 Turn it back on to respawn.重新打开以重生。

// Turn it off
gameObject.SetActive(false);

Instantiating is expensive and can cause framerate hitches.实例化很昂贵,并且可能导致帧率故障。

To expand on Hanger's response.扩展 Hanger 的回应。 You would use your tank variable like this to turn it off:你可以使用你的 tank 变量来关闭它:

tank1.SetActive(false);

Edit to include the second part of your title, the delay.编辑以包括标题的第二部分,即延迟。 Also, you'll want to set that new Vector3 in the respawn method to wherever you want to respawn the object at.此外,您还需要在 respawn 方法中将new Vector3设置为您想要重生 object 的任何位置。

GameObject tank1;
GameObject tank2;
public int bullet;


// Start is called before the first frame update
void Awake()
{
    tank1 = GameObject.Find("Tank1");
    tank2 = GameObject.Find("Tank2");

}
    // Toggles active status of GameObject passed to it
void ToggleActiveInScene(GameObject gameObject)
{
    gameObject.SetActive(!gameObject.activeSelf);
}

// If GameObject is disabled, enables and moves it
void Respawn(GameObject gameObject)
{
    if(gameObject.activeSelf == false)
    {
        ToggleActiveInScene(gameObject);
        gameObject.transform.position = new Vector3(7, -2.5f, -0.1f);
    }
}

// A coroutine to delay ToggleActiveInScene
float secondsToWait = 2;
IEnumerator delayedToggleActiveInScene(GameObject gameObject)
{
    yield return new WaitForSeconds(secondsToWait);
    ToggleActiveInScene(gameObject);
}

// // A coroutine to delay Respawn
IEnumerator delayedRespawn(GameObject gameObject)
{
    yield return new WaitForSeconds(secondsToWait);
    Respawn(gameObject);
}

void OnTriggerEnter2D(Collider2D other)
{
    //If the bullet from tank 1 hits the object
    if (bullet == 1)
    {
        if (other.tag == "Wall")
        {
            Destroy(this.gameObject);
        }

        if (other.tag == "Tank2")
        {
            Destroy(this.gameObject);

            // StartCoroutine will start the coroutines like this
            StartCoroutine(delayedToggleActiveInScene(other.gameObject));
            StartCoroutine(delayedRespawn(other.gameObject));
        }
    }

    //If the bullet from tank 2 hits the object
    else if (bullet == 2)
    {
        if (other.tag == "Wall")
        {
            Destroy(this.gameObject);
        }

        if (other.tag == "Tank1")
        {
            Destroy(this.gameObject);
            StartCoroutine(delayedToggleActiveInScene(other.gameObject));
            StartCoroutine(delayedRespawn(other.gameObject));
        }
    }
}

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

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