diff options
author | chai <215380520@qq.com> | 2023-10-27 11:05:14 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-10-27 11:05:14 +0800 |
commit | 766cdff5ffa72b65d7f106658d1603f47739b2ba (patch) | |
tree | 34d7799a94dfa9be182825577583c0fa6dc935f7 /GameCode/UnparentOnHit.cs |
+ init
Diffstat (limited to 'GameCode/UnparentOnHit.cs')
-rw-r--r-- | GameCode/UnparentOnHit.cs | 38 |
1 files changed, 38 insertions, 0 deletions
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<ProjectileHit>(); + 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); + } +} |