91 lines
3.1 KiB
C#
91 lines
3.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class PlayerInteractionController : MonoBehaviour
|
|
{
|
|
[Header("Detection Settings")]
|
|
[SerializeField] private float range = 3f;
|
|
[SerializeField] private LayerMask interactableLayer; // Tunnel용 레이어
|
|
[SerializeField] private LayerMask constructionLayer; // 건설 토대용 레이어
|
|
|
|
[Header("Build Settings")]
|
|
[SerializeField] private float buildSpeedMultiplier = 2f;
|
|
|
|
private PlayerInputActions _inputActions;
|
|
private bool _isHoldingInteract = false;
|
|
|
|
void Awake()
|
|
{
|
|
_inputActions = new PlayerInputActions();
|
|
|
|
// 탭(짧게 누르기) 시점 체크
|
|
_inputActions.Player.Interact.performed += OnInteractTap;
|
|
|
|
// 홀드(꾹 누르기) 시작/종료 체크
|
|
_inputActions.Player.Interact.started += ctx => {
|
|
_isHoldingInteract = true;
|
|
Debug.Log("인터랙션 버튼 누르기 시작 (건설 가속 준비)");
|
|
};
|
|
_inputActions.Player.Interact.canceled += ctx => {
|
|
_isHoldingInteract = false;
|
|
Debug.Log("인터랙션 버튼 뗌");
|
|
};
|
|
}
|
|
|
|
void OnEnable() => _inputActions.Enable();
|
|
void OnDisable() => _inputActions.Disable();
|
|
|
|
void Update()
|
|
{
|
|
if (_isHoldingInteract)
|
|
{
|
|
PerformConstructionSupport();
|
|
}
|
|
}
|
|
|
|
private void OnInteractTap(InputAction.CallbackContext context)
|
|
{
|
|
Debug.Log("E 키 탭 감지! 주변 탐색 시작...");
|
|
|
|
// 1. 주변 모든 콜라이더 가져오기 (레이어 마스크 없이 먼저 테스트해보고 싶다면 0 대신 ~0 입력)
|
|
Collider[] colliders = Physics.OverlapSphere(transform.position, range, interactableLayer);
|
|
|
|
Debug.Log($"주변 {interactableLayer.value} 레이어에서 {colliders.Length}개의 오브젝트 감지됨");
|
|
|
|
foreach (var col in colliders)
|
|
{
|
|
// 2. 인터페이스 찾기 (본인 또는 부모에게서)
|
|
IInteractable interactable = col.GetComponentInParent<IInteractable>();
|
|
if (interactable != null)
|
|
{
|
|
Debug.Log($"[성공] {col.name}에서 IInteractable 발견! 터널 진입합니다.");
|
|
interactable.Interact(gameObject);
|
|
_isHoldingInteract = false; // 이동 중에는 건설 지원 중단
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"[실패] {col.name} 감지되었으나 IInteractable 스크립트가 없음");
|
|
}
|
|
}
|
|
}
|
|
|
|
private void PerformConstructionSupport()
|
|
{
|
|
Collider[] targets = Physics.OverlapSphere(transform.position, range, constructionLayer);
|
|
foreach (var col in targets)
|
|
{
|
|
ConstructionSite site = col.GetComponent<ConstructionSite>();
|
|
if (site != null)
|
|
{
|
|
site.AdvanceConstruction(Time.deltaTime * buildSpeedMultiplier);
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnDrawGizmosSelected()
|
|
{
|
|
Gizmos.color = Color.yellow;
|
|
Gizmos.DrawWireSphere(transform.position, range);
|
|
}
|
|
} |