Files

57 lines
1.4 KiB
C#
Raw Permalink Normal View History

2026-06-04 20:14:47 +03:00
using UnityEngine;
public class PlayerCombat : MonoBehaviour
{
public float attackRange = 3f;
public int damage = 100;
public KeyCode keyboardAttack = KeyCode.F;
private void Update()
{
bool uiBlocksCombat =
DialogueUI.Instance != null && DialogueUI.Instance.IsOpen ||
QuestLogUI.Instance != null && QuestLogUI.Instance.IsOpen;
if (uiBlocksCombat)
{
return;
}
if (Input.GetKeyDown(keyboardAttack) || Input.GetMouseButtonDown(0))
{
AttackNearestEnemy();
}
}
private void AttackNearestEnemy()
{
Enemy nearest = null;
float nearestDistance = attackRange;
Enemy[] enemies = FindObjectsOfType<Enemy>();
foreach (Enemy enemy in enemies)
{
if (enemy == null || enemy.IsDead)
{
continue;
}
float distance = Vector3.Distance(transform.position, enemy.transform.position);
if (distance <= nearestDistance)
{
nearestDistance = distance;
nearest = enemy;
}
}
if (nearest != null)
{
nearest.TakeDamage(damage);
}
else if (NotificationUI.Instance != null)
{
NotificationUI.Instance.Show("Нет врага в радиусе атаки");
}
}
}