건물 건설 모드 및 건설 인터랙션
This commit is contained in:
58
Assets/Scripts/Player/PlayerBuildInteract.cs
Normal file
58
Assets/Scripts/Player/PlayerBuildInteract.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem; // New Input System 사용
|
||||
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
private Rigidbody _rb;
|
||||
private Vector2 _inputVector;
|
||||
[SerializeField] private float moveSpeed = 5f;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_rb = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
// Player Input 컴포넌트의 Behavior가 'Send Messages'일 때 자동 호출됨
|
||||
// Input Action 이름이 "Move"라면 "OnMove"라는 메서드를 찾습니다.
|
||||
void OnMove(InputValue value)
|
||||
{
|
||||
_inputVector = value.Get<Vector2>();
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
// 물리 연산은 프레임 독립적인 FixedUpdate에서 수행
|
||||
Move();
|
||||
}
|
||||
|
||||
void Move()
|
||||
{
|
||||
Vector3 movement = new Vector3(_inputVector.x, 0, _inputVector.y) * moveSpeed;
|
||||
|
||||
// Rigidbody의 속도를 직접 제어하거나 MovePosition 사용
|
||||
// 등속도 이동을 위해 velocity의 x, z만 변경 (y는 중력 유지를 위해 기존 값 사용)
|
||||
_rb.linearVelocity = new Vector3(movement.x, _rb.linearVelocity.y, movement.z);
|
||||
}
|
||||
}
|
||||
36
Assets/Scripts/Player/PlayerMovement.cs
Normal file
36
Assets/Scripts/Player/PlayerMovement.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float moveSpeed = 5f;
|
||||
private Vector2 _moveInput;
|
||||
private CharacterController _controller;
|
||||
private PlayerInputActions _inputActions;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_controller = GetComponent<CharacterController>();
|
||||
_inputActions = new PlayerInputActions();
|
||||
|
||||
// Move 액션 연결 (Vector2 타입으로 설정되어 있어야 함)
|
||||
_inputActions.Player.Move.performed += ctx => _moveInput = ctx.ReadValue<Vector2>();
|
||||
_inputActions.Player.Move.canceled += ctx => _moveInput = Vector2.zero;
|
||||
}
|
||||
|
||||
void OnEnable() => _inputActions.Enable();
|
||||
void OnDisable() => _inputActions.Disable();
|
||||
|
||||
void Update()
|
||||
{
|
||||
// 입력받은 방향으로 이동 계산
|
||||
Vector3 move = new Vector3(_moveInput.x, 0, _moveInput.y);
|
||||
_controller.Move(move * Time.deltaTime * moveSpeed);
|
||||
|
||||
// 이동 중일 때 바라보는 방향 전환 (선택 사항)
|
||||
if (move != Vector3.zero)
|
||||
{
|
||||
transform.forward = move;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user