36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
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);
|
|
}
|
|
} |