using UnityEngine;
public class EnemyBullet : MonoBehaviour
{
public float speed = 5f; // bullet speed
public float lifeTime = 5f; // auto-destroy after seconds
void Start()
{
Destroy(gameObject, lifeTime); // destroy bullet automatically
}
void Update()
{
// move bullet downward
transform.Translate(Vector2.down * speed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D collision)
{
// destroy bullet if it hits player
if (collision.CompareTag("Player"))
{
Destroy(gameObject);
}
// destroy bullet if it hits walls or obstacles
{
Destroy(gameObject);
}
}
}