인터랙션 시스템 및 터널 기능 생성

This commit is contained in:
2026-01-15 16:45:14 +09:00
parent f70223a4aa
commit b8ecb9a24c
12 changed files with 959 additions and 37 deletions

View File

@@ -0,0 +1,79 @@
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);
}
}