Files
ProjectMD/Assets/Scripts/Player/PlayerBuildInteract.cs

58 lines
1.8 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerBuildInteract : MonoBehaviour
{
[Header("Interaction Settings")]
[SerializeField] private float interactRange = 3f; // 건설 가능 거리
[SerializeField] private float buildSpeedMultiplier = 1f; // 건설 속도 배율
[SerializeField] private LayerMask constructionLayer; // 토대 레이어 (선택 사항)
private PlayerInputActions _inputActions;
private bool _isInteracting = false;
void Awake()
{
_inputActions = new PlayerInputActions();
// Interact 액션 연결 (Hold 방식)
_inputActions.Player.Interact.started += ctx => _isInteracting = true;
_inputActions.Player.Interact.canceled += ctx => _isInteracting = false;
}
void OnEnable() => _inputActions.Enable();
void OnDisable() => _inputActions.Disable();
void Update()
{
// 키를 누르고 있을 때만 실행
if (_isInteracting)
{
PerformConstruction();
}
}
void PerformConstruction()
{
// 주변의 모든 콜라이더 검사
Collider[] targets = Physics.OverlapSphere(transform.position, interactRange, 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, interactRange);
}
}