Attacks on collisions

This commit is contained in:
2025-08-07 02:02:46 +02:00
parent 4e128b7641
commit 9a70b0bd2a
5 changed files with 118 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
using UnityEngine;
public class EnemyAttack : MonoBehaviour
{
[SerializeField] private float attackRange = 5f;
[SerializeField] private float attackRate = 2f;
[SerializeField] private float attackDamage = 10f;
[SerializeField] private float attackAngle = 45f;
private float lastAttackTime = 0f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
}
private void OnTriggerEnter(Collider other)
{
}
private void OnTriggerStay(Collider other)
{
var player = other.GetComponent<PlayerMovement>();
if (player != null && Time.time - lastAttackTime > attackRate)
{
var health = other.GetComponent<HealthManager>();
if (health != null)
{
health.ModifyHealth(-attackDamage);
Debug.Log("Enemy attacked! Next attack in: " + (1f / attackRate) + " seconds");
lastAttackTime = Time.time;
}
}
}
private void OnTriggerExit(Collider other)
{
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 333830e3ba10f2942b70a753c4c281de

View File

@@ -55,6 +55,8 @@ public class HealthManager : MonoBehaviour
return;
}
_currentHealth += amount;
Debug.Log("Health of " + gameObject.name + ": " + _currentHealth);
}
}