MOD: Enemy AI

MOD: Player Shooting
MOD: Damage Player and Enemy
This commit is contained in:
2024-09-05 20:40:50 +02:00
parent 4d0e81695d
commit 1eeafca3f2
36 changed files with 1072 additions and 508 deletions

View File

@@ -1,18 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

View File

@@ -0,0 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovementMelee : MonoBehaviour
{
public GameObject player;
public EnemyStats enemyStats;
public float targetDistance;
private float distance;
// Start is called before the first frame update
void Start()
{
enemyStats = GetComponent<EnemyStats>();
}
// Update is called once per frame
void Update()
{
if(player != null){
// Calculate the distance between the enemy and the player
distance = Vector2.Distance(transform.position, player.transform.position);
// Get the direction to the player by subtracting the enemy's position from the player's position
Vector2 direction = player.transform.position - transform.position;
direction.Normalize(); // Normalize the direction vector
// Calculate the angle to rotate the enemy to face the player
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
// If the player is within a certain range, move towards them
if (distance < targetDistance)
{
// Move the enemy towards the player's position
transform.position = Vector2.MoveTowards(transform.position, player.transform.position, enemyStats.speed * Time.deltaTime);
// Rotate the enemy to face the player
transform.rotation = Quaternion.Euler(Vector3.forward * angle);
}
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 58a007a46183a8947a29e53b7c347d41
guid: 52630bacc6c11024e9cbe915465fc6d0
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -8,16 +8,31 @@ public class EnemyStats : MonoBehaviour
public Enemy enemy;
public float health = 0;
public float speed = 0;
public float damage = 0;
private void Start() {
health = enemy.currentHealth;
speed = enemy.currentSpeed;
health = enemy.getCurrentHealth();
speed = enemy.getCurrentSpeed();
damage = enemy.getDamage();
}
//Für Später
/* private void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.CompareTag("Sword") || other.gameObject.CompareTag("Bullet")){
private void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.CompareTag("Bullet")){
BulletScript bulletScript = other.gameObject.GetComponent<BulletScript>();
health -= bulletScript.weapon.getDamage();
if (health <= 0)
{
Destroy(this.gameObject);
}
} else if(other.gameObject.CompareTag("Player")){
speed = 0f;
}
} */
}
private void OnTriggerExit2D(Collider2D other) {
speed = enemy.getCurrentSpeed();
}
}