[英]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.