[英]How to respawn the tank after being destroyed in unity? (with a few seconds delay)
我目前實現的function是兩輛坦克可以互相射擊,互相摧毀,被摧毀后無法重生。 我試圖在摧毀它后再次實例化坦克,但是當我這樣做時,實例化的object失去了它的所有屬性。(它不再變得可控或破壞)我的目標是當子彈擊中對手的坦克時,它會摧毀對手. 對方會在4秒延遲后立即消失並在默認的position(或任意位置)中復活。
這是我的代碼:
公共 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()
{
}
}
不要破壞坦克。 把它關掉。 重新打開以重生。
// Turn it off
gameObject.SetActive(false);
實例化很昂貴,並且可能導致幀率故障。
擴展 Hanger 的回應。 你可以使用你的 tank 變量來關閉它:
tank1.SetActive(false);
編輯以包括標題的第二部分,即延遲。 此外,您還需要在 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.