UnityGameProjectsCode / InTheDarkGame / Level / Drone / DroneAIModSound.cs
DroneAIModSound.cs
Raw
using System.Collections.Generic;
using UnityEngine;

public class DroneAIModSound : DroneAIMod
{
    /* every set seconds the bot will search within a radius
     * for sound markers that are generated by environment objects.
     * If a marker is found within range, the bot will navigate
     * to the first one it finds OR if there is more than one
     * it will navigate to the closest one.
     */


    public float soundDetectionRange;
    public float detectionScanWaitTime;
    private float timeBetweenScans;

    private void Update()
    {
        if (GetMoveTarget() != GetPlayerObject())
        {
            timeBetweenScans += Time.deltaTime;

            if (timeBetweenScans >= detectionScanWaitTime)
            {
                List<GameObject> markers = new List<GameObject>();

                foreach (GameObject marker in GameObject.FindGameObjectsWithTag("Sound Marker"))
                {
                    if (Vector2.Distance(marker.transform.position, transform.position) <= soundDetectionRange)
                    {
                        markers.Add(marker);
                    }
                }

                if (markers.Count > 1)
                {
                    SetCustomTarget(FindClosestObject(markers));
                }

                if (markers.Count == 1)
                {
                    SetCustomTarget(markers[0]);
                }

                timeBetweenScans = 0.0f;
            }
        }
    }
}