summaryrefslogtreecommitdiff
path: root/GameCode/UnparentOnHit.cs
blob: 50266c7e5197493af0ca9699d7f3050942a1cd7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);
	}
}