69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public class EnemyAttack : MonoBehaviour
|
|
{
|
|
[Header("Attack Settings")]
|
|
public float damage = 10f;
|
|
public float attackRange = 2.0f;
|
|
public float attackCooldown = 1.0f;
|
|
|
|
private Material _enemyMaterial;
|
|
private float _nextAttackTime;
|
|
private Transform _target;
|
|
private IDamageable _targetDamageable;
|
|
|
|
// 쉐이더 프로퍼티 ID (문자열보다 성능이 좋음)
|
|
private static readonly int EmissionColorProp = Shader.PropertyToID("_EmissionColor");
|
|
|
|
void Update()
|
|
{
|
|
// 이동 스크립트로부터 현재 목표를 실시간으로 가져옵니다.
|
|
if (_target == null)
|
|
{
|
|
// EnemyMoveDefault에서 _target을 public이나 프로퍼티로 만들어 가져오세요
|
|
_target = GetComponent<EnemyMoveDefault>().Target;
|
|
if (_target != null) _targetDamageable = _target.GetComponent<IDamageable>();
|
|
return;
|
|
}
|
|
|
|
float distance = Vector3.Distance(transform.position, _target.position);
|
|
|
|
// 사거리 안에 있고 타겟이 살아있을 때만 공격
|
|
if (distance <= attackRange)
|
|
{
|
|
if (Time.time >= _nextAttackTime)
|
|
{
|
|
Attack();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Attack()
|
|
{
|
|
if (_target == null) return;
|
|
|
|
if (_targetDamageable == null)
|
|
{
|
|
_targetDamageable = _target.GetComponentInParent<IDamageable>();
|
|
}
|
|
|
|
if (_targetDamageable != null)
|
|
{
|
|
_targetDamageable.TakeDamage(damage);
|
|
_nextAttackTime = Time.time + attackCooldown;
|
|
}
|
|
else
|
|
{
|
|
// 대상이 IDamageable이 아니거나 이미 파괴됨
|
|
_target = null;
|
|
}
|
|
}
|
|
|
|
public void SetAttackTarget(Transform newTarget)
|
|
{
|
|
_target = newTarget;
|
|
if (_target != null)
|
|
_targetDamageable = _target.GetComponent<IDamageable>();
|
|
}
|
|
} |