简体   繁体   中英

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. 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. The opponent will immediately disappear and resurrect in the default position (or any position) after 4 seconds delay.

Here is my code:

public class Bullet: 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. You would use your tank variable like this to turn it off:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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