diff options
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); + } +} |