2020-05-31 17:07:22 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class OutOfRangeDestroyer : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public float timeUntilDestroyed = 5;
|
|
|
|
|
public float range = 250;
|
|
|
|
|
public Transform targetObject = null;
|
|
|
|
|
|
|
|
|
|
private float timeSinceOutOfRange = 0;
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
2019-05-20 09:57:42 +00:00
|
|
|
|
if (targetObject == null)
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
2019-05-20 09:57:42 +00:00
|
|
|
|
if (Camera.main != null)
|
|
|
|
|
targetObject = Camera.main.transform;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
timeSinceOutOfRange += Time.deltaTime;
|
|
|
|
|
|
2019-05-20 09:57:42 +00:00
|
|
|
|
if (targetObject == null)
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
2019-05-20 09:57:42 +00:00
|
|
|
|
if (Camera.main != null)
|
|
|
|
|
targetObject = Camera.main.transform;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-20 09:57:42 +00:00
|
|
|
|
if (targetObject != null)
|
2020-05-31 17:07:22 +00:00
|
|
|
|
{
|
|
|
|
|
float distanceSq = (transform.position - targetObject.position).sqrMagnitude;
|
|
|
|
|
if (distanceSq <= range * range)
|
|
|
|
|
{
|
|
|
|
|
timeSinceOutOfRange = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timeSinceOutOfRange >= timeUntilDestroyed)
|
|
|
|
|
{
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|