46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
public class WeaponHitBox : MonoBehaviour
|
|
{
|
|
private float _damage;
|
|
private bool _isActive = false; // ⭐ 기본은 꺼져 있어야 합니다!
|
|
private List<IDamageable> _hitTargets = new List<IDamageable>();
|
|
|
|
public void EnableHitBox(float damage)
|
|
{
|
|
_damage = damage;
|
|
_isActive = true; // 이제부터 공격 가능
|
|
_hitTargets.Clear();
|
|
gameObject.SetActive(true); // 오브젝트도 함께 켜줍니다.
|
|
}
|
|
|
|
public void DisableHitBox()
|
|
{
|
|
_isActive = false; // 공격 불가능
|
|
gameObject.SetActive(false); // 오브젝트도 꺼줍니다.
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
// 1. 공격 활성화 상태가 아니면 무시 (근접 킬 방지)
|
|
if (!_isActive) return;
|
|
|
|
// 2. ⭐ [자해 방지] 부딪힌 대상이 나(Player)라면 무시합니다!
|
|
if (other.CompareTag("Player")) return;
|
|
|
|
// 3. 적(IDamageable)에게 데미지 입히기
|
|
if (other.TryGetComponent<IDamageable>(out var target))
|
|
{
|
|
if (!_hitTargets.Contains(target))
|
|
{
|
|
target.TakeDamage(_damage);
|
|
_hitTargets.Add(target);
|
|
Debug.Log($"<color=green>[Hit]</color> {other.name}에게 {_damage} 데미지!");
|
|
|
|
// 타격 시 효과 (카메라 쉐이크 등 호출)
|
|
SendMessageUpwards("OnAttackShake", SendMessageOptions.DontRequireReceiver);
|
|
}
|
|
}
|
|
}
|
|
} |