using UnityEngine; using RR.Game2D.Entities.Projectiles; namespace RR.Game2D.Behaviour { public class EnemyFireScript : MonoBehaviour { [Header("Снаряд")] [SerializeField] private BasicProjectile projectilePrefab; [SerializeField] private float projectileLifetime = 1f; [SerializeField] private float throwCooldown = 5f; [SerializeField] private float throwSpeed = 20f; private float lastThrowTime = -999f; private BasicProjectile projectile; void Start() { lastThrowTime = Time.time; } void Update() { if (!projectilePrefab) { Debug.LogError("EnemyFireScript: projectile prefab missing; assign projectile prefab in the inspector"); return; } if (projectile != null && Time.time - lastThrowTime >= projectile.maxLifetime) projectile.Despawn(); // Бросок if (Time.time - lastThrowTime >= throwCooldown) Throw(); } private void OnDestroy() { // При смерти врага сго снаряд тоже пропадает if (projectile != null) projectile.Despawn(); } /// /// Метод для броска снаряда (выстрела). /// У снаряда обязательно должен быть метод Launch(Vector2 direction) /// private void Throw() { float rotationY = transform.localEulerAngles.y; Vector2 throwDirection = rotationY >= 180f ? Vector2.right : Vector2.left; Collider2D enemyCol = GetComponent(); Vector2 throwPosition = new(1.4f * throwDirection.x + transform.position.x, enemyCol.bounds.center.y); projectile = Instantiate(projectilePrefab, throwPosition, Quaternion.identity); projectile.maxLifetime = projectileLifetime; Collider2D projectileCol = projectile.GetComponent(); projectile.transform.localRotation = throwDirection.x < 0 ? Quaternion.Inverse(projectile.transform.localRotation) : projectile.transform.localRotation; projectile.transform.position = throwPosition; projectile.Launch(throwDirection, throwSpeed); if (enemyCol && projectileCol) Physics2D.IgnoreCollision(enemyCol, projectileCol, true); lastThrowTime = Time.time; } } }