(Unity)一定時間エリア内に止まると、敵を破壊することができる。

(方向性)

 


(スクリプト)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroySpawn : MonoBehaviour
{
    public float timeCount;
    public GameObject target;
    public GameObject effectPrefab;
    public AudioSource audioSource;

    private void OnTriggerStay(Collider other)
    {
        if(other.gameObject.tag == "Player")
        {
            timeCount += Time.deltaTime;
            audioSource.enabled = true;

            if(timeCount > 10f)
            {
                Destroy(target.gameObject);
                GameObject effect = Instantiate(effectPrefab, target.transform.position, Quaternion.identity);
                Destroy(effect, 1.0f);
                this.gameObject.SetActive(false);
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        timeCount = 0;
        audioSource.enabled = false;
    }
}

(実行結果)