68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Enemie_Movement : MonoBehaviour
|
|
{
|
|
public float speed = 3f; // Bewegungsgeschwindigkeit
|
|
public float shootingInterval = 2f; // Schuss-Intervall
|
|
public int life = 5;
|
|
public GameObject CDM_Enemy_LASER_bullet; // Projektil-Objekt
|
|
public Transform firePoint; // Punkt, von dem die Kugeln abgefeuert werden
|
|
Enemie_Spawn_System enemie_Spawn_System;
|
|
private Transform player; // Referenz auf den Spieler
|
|
|
|
void Start()
|
|
{
|
|
// Spieler-Objekt finden
|
|
player = GameObject.FindGameObjectWithTag("Starship").transform;
|
|
enemie_Spawn_System = GameObject.Find("Enemie_Spawner_System").GetComponent<Enemie_Spawn_System>();
|
|
|
|
// Starte das Schießen in Intervallen
|
|
InvokeRepeating(nameof(Shoot), shootingInterval, shootingInterval);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (player == null) return; // Falls kein Spieler gefunden wurde, nichts tun
|
|
|
|
// 1. Richtung zum Spieler berechnen
|
|
Vector2 direction = (player.position - transform.position).normalized;
|
|
|
|
// 2. Gegner drehen (damit er den Spieler anvisiert)
|
|
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
|
|
transform.rotation = Quaternion.Euler(0, 0, angle - 90);
|
|
|
|
// 3. Bewegung zum Spieler
|
|
transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
|
|
}
|
|
|
|
void Shoot()
|
|
{
|
|
if (CDM_Enemy_LASER_bullet != null && firePoint != null)
|
|
{
|
|
Instantiate(CDM_Enemy_LASER_bullet, firePoint.position, firePoint.rotation);
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D other)
|
|
{
|
|
if (other.CompareTag("Player Bullet"))
|
|
{
|
|
Bullet_movement bulletScript = other.gameObject.GetComponent<Bullet_movement>();
|
|
|
|
if (bulletScript != null)
|
|
{
|
|
life--;
|
|
Debug.Log("Gegner Getroffen! -1");
|
|
|
|
if (life <= 0)
|
|
{
|
|
Destroy(gameObject);
|
|
enemie_Spawn_System.ChangeEnemyCounter(-1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|