Enemy의 이동 및 공격 로직 개선
포탈 추가
This commit is contained in:
@@ -1,26 +1,69 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemyAttack : MonoBehaviour
|
||||
{
|
||||
[Header("Attack Settings")]
|
||||
[SerializeField] private float damage = 10f;
|
||||
[SerializeField] private float attackCooldown = 1.0f; // 공격 간격 (1초)
|
||||
public float damage = 10f;
|
||||
public float attackRange = 2.0f;
|
||||
public float attackCooldown = 1.0f;
|
||||
|
||||
private float _nextAttackTime = 0f;
|
||||
private Material _enemyMaterial;
|
||||
private float _nextAttackTime;
|
||||
private Transform _target;
|
||||
private IDamageable _targetDamageable;
|
||||
|
||||
// EnemyAttack.cs
|
||||
private void OnCollisionStay(Collision collision)
|
||||
// 쉐이더 프로퍼티 ID (문자열보다 성능이 좋음)
|
||||
private static readonly int EmissionColorProp = Shader.PropertyToID("_EmissionColor");
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Time.time >= _nextAttackTime)
|
||||
// 이동 스크립트로부터 현재 목표를 실시간으로 가져옵니다.
|
||||
if (_target == null)
|
||||
{
|
||||
// 상대방에게서 IDamageable 인터페이스가 있는지 확인
|
||||
IDamageable target = collision.gameObject.GetComponent<IDamageable>();
|
||||
// EnemyMoveDefault에서 _target을 public이나 프로퍼티로 만들어 가져오세요
|
||||
_target = GetComponent<EnemyMoveDefault>().Target;
|
||||
if (_target != null) _targetDamageable = _target.GetComponent<IDamageable>();
|
||||
return;
|
||||
}
|
||||
|
||||
if (target != null)
|
||||
float distance = Vector3.Distance(transform.position, _target.position);
|
||||
|
||||
// 사거리 안에 있고 타겟이 살아있을 때만 공격
|
||||
if (distance <= attackRange)
|
||||
{
|
||||
if (Time.time >= _nextAttackTime)
|
||||
{
|
||||
target.TakeDamage(damage);
|
||||
_nextAttackTime = Time.time + attackCooldown;
|
||||
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>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user