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/UnparentOnHit.cs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 GameCode/UnparentOnHit.cs (limited to 'GameCode/UnparentOnHit.cs') diff --git a/GameCode/UnparentOnHit.cs b/GameCode/UnparentOnHit.cs new file mode 100644 index 0000000..50266c7 --- /dev/null +++ b/GameCode/UnparentOnHit.cs @@ -0,0 +1,38 @@ +using System.Collections; +using UnityEngine; +using UnityEngine.Events; + +public class UnparentOnHit : MonoBehaviour +{ + public float destroyAfterSeconds = 2f; + + public UnityEvent unparentEvent; + + private bool done; + + private void Start() + { + ProjectileHit componentInParent = GetComponentInParent(); + if ((bool)componentInParent) + { + componentInParent.AddHitAction(Unparent); + } + } + + public void Unparent() + { + if (!done) + { + done = true; + base.transform.SetParent(null, worldPositionStays: true); + StartCoroutine(DelayDestroy()); + unparentEvent.Invoke(); + } + } + + private IEnumerator DelayDestroy() + { + yield return new WaitForSeconds(destroyAfterSeconds); + Object.Destroy(base.gameObject); + } +} -- cgit v1.1-26-g67d0