79 lines
2.6 KiB
C#
79 lines
2.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem; // New Input System 네임스페이스
|
|
|
|
public class PlayerInteraction : MonoBehaviour
|
|
{
|
|
[Header("Detection Settings")]
|
|
[SerializeField] private float interactionRadius = 2.5f;
|
|
[SerializeField] private LayerMask interactableLayer;
|
|
|
|
private PlayerInputActions _inputActions;
|
|
|
|
void Awake()
|
|
{
|
|
_inputActions = new PlayerInputActions();
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
// Interact 액션이 수행되었을 때(버튼을 눌렀을 때) 실행될 함수 연결
|
|
_inputActions.Player.Interact.performed += OnInteractPerformed;
|
|
_inputActions.Enable();
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
// 이벤트 연결 해제 및 비활성화
|
|
_inputActions.Player.Interact.performed -= OnInteractPerformed;
|
|
_inputActions.Disable();
|
|
}
|
|
|
|
// Input Action 콜백 함수
|
|
private void OnInteractPerformed(InputAction.CallbackContext context)
|
|
{
|
|
Debug.Log("E 키 눌림!"); // <-- 이게 콘솔에 찍히나요?
|
|
|
|
// 건설 모드 중일 때는 상호작용을 막고 싶다면 아래 조건 추가
|
|
if (BuildManager.Instance.IsBuildMode) return;
|
|
|
|
CheckAndInteract();
|
|
}
|
|
|
|
private void CheckAndInteract()
|
|
{
|
|
Collider[] colliders = Physics.OverlapSphere(transform.position, interactionRadius, interactableLayer);
|
|
Debug.Log($"주변에서 {colliders.Length}개의 물체 감지됨"); // 0이 나오면 레이어나 콜라이더 문제
|
|
|
|
IInteractable nearestInteractable = null;
|
|
float minDistance = Mathf.Infinity;
|
|
|
|
foreach (var col in colliders)
|
|
{
|
|
// 부모까지 포함하여 IInteractable 인터페이스를 찾음
|
|
IInteractable interactable = col.GetComponentInParent<IInteractable>();
|
|
|
|
if (interactable != null)
|
|
{
|
|
Debug.Log($"{col.name}에서 인터페이스 발견!");
|
|
float distance = Vector3.Distance(transform.position, col.transform.position);
|
|
if (distance < minDistance)
|
|
{
|
|
minDistance = distance;
|
|
nearestInteractable = interactable;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (nearestInteractable != null)
|
|
{
|
|
nearestInteractable.Interact(gameObject);
|
|
Debug.Log($"[Interaction] {nearestInteractable.GetInteractionText()} 실행");
|
|
}
|
|
}
|
|
|
|
private void OnDrawGizmosSelected()
|
|
{
|
|
Gizmos.color = Color.yellow;
|
|
Gizmos.DrawWireSphere(transform.position, interactionRadius);
|
|
}
|
|
} |