From 766cdff5ffa72b65d7f106658d1603f47739b2ba Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 27 Oct 2023 11:05:14 +0800 Subject: + init --- GameCode/Unparent.cs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 GameCode/Unparent.cs (limited to 'GameCode/Unparent.cs') diff --git a/GameCode/Unparent.cs b/GameCode/Unparent.cs new file mode 100644 index 0000000..cc69c83 --- /dev/null +++ b/GameCode/Unparent.cs @@ -0,0 +1,44 @@ +using System.Collections; +using UnityEngine; + +public class Unparent : MonoBehaviour +{ + public Transform parent; + + public bool follow; + + public float destroyDelay; + + private bool done; + + private void Start() + { + parent = base.transform.root; + } + + private void LateUpdate() + { + if (!done) + { + if (base.transform.root != null) + { + base.transform.SetParent(null, worldPositionStays: true); + } + if (follow && (bool)parent) + { + base.transform.position = parent.transform.position; + } + if (!parent) + { + StartCoroutine(DelayRemove()); + done = true; + } + } + } + + private IEnumerator DelayRemove() + { + yield return new WaitForSeconds(destroyDelay); + Object.Destroy(base.gameObject); + } +} -- cgit v1.1-26-g67d0