fgm24/Assets/Scripts/Enemy/EnemyProjectile.cs

28 lines
708 B
C#
Raw Normal View History

2024-02-03 01:41:20 +01:00
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<Rigidbody2D>();
}
private void Update()
{
target = GetComponent<ShootingRange>().target;
if (target != null)
FireProjectile();
}
void FireProjectile()
{
Vector2 directionToTarget = (target.transform.position - transform.position).normalized;
rb.velocity = directionToTarget * speed;
Destroy(this, projectileLifetime);
}
}