This repository has been archived on 2024-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
sota/RunningLateGame/Assets/Scripts/AiManager.cs

43 lines
1.1 KiB
C#
Raw Normal View History

2024-08-08 16:11:12 +08:00
using System.Collections;
using UnityEngine;
public class AiManager : MonoBehaviour
{
public Transform player;
public float cullingDistance;
public GameObject aiPrefab;
private GameObject[] _ais;
private float _distance;
public int maxAI;
private void Start()
{
StartCoroutine(Manager());
}
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(player.position, cullingDistance);
}
// ReSharper disable Unity.PerformanceAnalysis
2024-08-08 16:11:12 +08:00
private IEnumerator Manager()
{
while (true)
{
_ais = GameObject.FindGameObjectsWithTag("AIs");
if (_ais.Length < maxAI )
{
var instance = Instantiate(aiPrefab, gameObject.transform);
}
foreach (var i in _ais)
{
_distance = Vector3.Distance(i.transform.position, player.position);
if (_distance > cullingDistance) Destroy(i.gameObject);
}
yield return new WaitForSeconds(1);
}
2024-08-08 16:11:12 +08:00
}
}