[英]UNITY SCRIPTS:spawn in same location after getting destroyed(after 3 seconds)
嘿,有谁知道如何为对象在被子弹摧毁后重新生成 C# 脚本
There are multible ways to archive this.有多种方法可以存档。
Note that you can also work completely without Coroutine but this usually sucks请注意,您也可以在没有协程的情况下完全工作,但这通常很糟糕
private float timer;
private bool isRespawning;
private GameObject clone;
private void DestroyObject(GameObject obj)
{
isRespawning = true;
timer = 3f;
// first make a clone of the current Object
var clone = Instantiate(obj, obj.transform.position, obj.transform.rotation, objtransform.parent);
// disable the clone
clone.SetActive(false);
// Destroy the original
Destroy(obj);
}
private void Update()
{
if(Mathf.Approximately(timer,0f)) return;
timer += Time.deltaTime;
if(timer > 0) return;
// Set the clone active
clone.SetActive(true);
timer = 0;
}
Simplier would be a Coroutine:更简单的是协程:
private void DestroyAndRespawn(GameObject obj)
{
StartCoroutine(DestroyAndRespawnRoutine(obj));
}
private IEnumerator DestroyAndRespawnRoutine(GameObject obj)
{
// first make a clone of the current Object
var clone = Instantiate(obj, obj.transform.position, obj.transform.rotation, objtransform.parent);
// disable the clone
clone.SetActive(false);
// Destroy the original
Destroy(obj);
// Wait 3 seconds
yield return new WaitForSeconds(3f);
// Set the clone active
clone.SetActive(true);
}
But why should you destroy the object and respawn it?但是为什么要销毁对象并重新生成它呢? You can simply only disable and later re enable it您可以简单地仅禁用然后重新启用它
private void DestroyAndRespawn(GameObject obj)
{
StartCoroutine(DestroyAndRespawnRoutine(obj));
}
private IEnumerator DestroyAndRespawnRoutine(GameObject obj)
{
// disable the obj
obj.SetActive(false);
// Wait 3 seconds
yield return new WaitForSeconds(3f);
// Set the objactive
obj.SetActive(true);
}
Update更新
Since you want to call this on collision:由于您想在碰撞时调用它:
You will need another GameObject that works as a Manager for the respawning / enabling, disabling since you can not just attach that script to the object you disable -> would also disable the script and never re-enable the object.您将需要另一个 GameObject 作为重生/启用、禁用的管理器,因为您不能将该脚本附加到您禁用的对象 -> 也会禁用脚本并且永远不会重新启用该对象。
Create a new Script RespawnManager.cs:创建一个新的脚本 RespawnManager.cs:
public class RespawnManager : MonoBehaviour { public static RespawnManager Singleton; private void Awake() { if(Singleton) { enabled = false; return; } Singleton = this; } private void DestroyAndRespawn(GameObject obj) { StartCoroutine(DestroyAndRespawnRoutine(obj)); } private IEnumerator DestroyAndRespawnRoutine(GameObject obj) { // disable the obj obj.SetActive(false); // Wait 3 seconds yield return new WaitForSeconds(3f); // Set the objactive obj.SetActive(true); } }
Than you can call it on collision like比你可以在碰撞时调用它
OnCollisionEnter(Collision obj) { if (obj.gameObject.name != "bullet") return; // pass this GameObject to the manager RespawnManager.Singleton.DestroyAndRespawn(gameObject); }
If you use Destroy(yourObject);
如果你使用Destroy(yourObject);
, the object does get removed and you loose access. ,该对象确实会被删除并且您无法访问。 You could make your Object into a prefab and spawn it where you want by using GameObject obj = Instantiate(...);
您可以使用GameObject obj = Instantiate(...);
将您的对象制作成预制件并在您想要的地方生成它GameObject obj = Instantiate(...);
Obj contains the reference to the new spawned object and not to the prefab, which means you can later again use Destroy(obj);
Obj 包含对新生成的对象的引用,而不是对预制件的引用,这意味着您稍后可以再次使用Destroy(obj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.