DestroyAfter
Add this MonoBehaviour on an object to destroy it automatically (use it on bullet prefabs etc..)
Simple
using EODE.Wonderland;
using UnityEngine;
public class DestroyAfter : MonoBehaviour {
public float Delay = 1f;
async void Start() {
await new WaitForSeconds(Delay);
if (this == null) return;
if (gameObject != null) gameObject.SafeDestroy();
}
}
With animator
using EODE.Wonderland;
using UnityEngine;
public class DestroyAfter : MonoBehaviour {
public float Delay = 1f;
async void Start() {
// animator
var anim = GetComponent<Animator>();
if (anim != null) {
// if a Death parameter is found
if (anim.HasParameter("Death")) {
// wait for it
await new WaitUntil(() => anim.GetBool("Death"));
}
}
await new WaitForSeconds(Delay);
if (this == null) return;
if (gameObject != null) gameObject.SafeDestroy();
}
}
With VFX
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using EODE.Wonderland;
using UnityEngine;
public class DestroyAfterVFX : MonoBehaviour {
public UnityEngine.VFX.VisualEffect VFX;
public float Delay = 1f;
public string VFXRateName = "Intensity";
public float FadeDuration = 0.5f;
public float DestroyDelay = 3f;
void Start() {
StartCoroutine(StopRoutine());
}
IEnumerator StopRoutine() {
yield return new WaitForSeconds(Delay);
float rate = 1f;
DOTween.To(() => rate, v => {
rate = v;
VFX.SetFloat(VFXRateName, rate);
}, 0f, FadeDuration);
yield return new WaitForSeconds(0f);
yield return new WaitForSeconds(FadeDuration + DestroyDelay);
if (gameObject != null) gameObject.SafeDestroy();
}
}