Projext/Assets/5.TestScript/WePonHitBox.cs

46 lines
1.5 KiB
C#
Raw Normal View History

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