using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyProjectile : MonoBehaviour { private GameObject target; public float speed = 5f; public float projectileLifetime = 3f; Rigidbody2D rb; void Start() { rb = GetComponent(); } private void Update() { target = GetComponent().target; if (target != null) FireProjectile(); } void FireProjectile() { Vector2 directionToTarget = (target.transform.position - transform.position).normalized; rb.velocity = directionToTarget * speed; Destroy(this, projectileLifetime); } }