51 lines
1.3 KiB
C#
51 lines
1.3 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
|
|
public class EnemyShooter : MonoBehaviour
|
|
{
|
|
[Header("Стрельба")]
|
|
public Weapon weapon;
|
|
[SerializeField] private AimBase aim;
|
|
|
|
[Header("Интервалы")]
|
|
[SerializeField] private float minInterval = 1.5f;
|
|
[SerializeField] private float maxInterval = 3f;
|
|
[SerializeField] private float startDelay = 0f;
|
|
|
|
private Coroutine shootRoutine;
|
|
|
|
private void Start()
|
|
{
|
|
if (weapon == null) weapon = GetComponent<Weapon>();
|
|
if (aim == null) aim = GetComponent<AimBase>();
|
|
|
|
if (weapon == null)
|
|
{
|
|
Debug.LogError($"No weapon on {gameObject.name}");
|
|
return;
|
|
}
|
|
|
|
shootRoutine = StartCoroutine(ShootLoop());
|
|
}
|
|
|
|
private IEnumerator ShootLoop()
|
|
{
|
|
if (startDelay > 0) yield return new WaitForSeconds(startDelay);
|
|
|
|
while (true)
|
|
{
|
|
float interval = Random.Range(minInterval, maxInterval);
|
|
yield return new WaitForSeconds(interval);
|
|
|
|
if (weapon.CanShoot && aim != null && aim.Target != null)
|
|
{
|
|
weapon.Shoot();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (shootRoutine != null) StopCoroutine(shootRoutine);
|
|
}
|
|
} |