Input System 설치하기
Window-Package Manager - Input System 을 검색해서 설치를 해준다.
ProjectSetting - Player - Other Setting - Configuation 에서 어떤 Input System 을 사용할 지 선택이 가능하다
Old나 New 나 현재까지 유용하기 때문에
Both를 선택해서 사용할 것이다.
Input Action Asset 을 사용해서 캐릭터 움직이게 하기
Input Action을 생성하기 위해서는 Project 에서 + 버튼을 누르면 다양한 생성 Asset들이 나온다
다양한 Asset들 중 최하단에 있는 Input Actions를 선택해서 InputActions을 하나 만들어준다.
생성한 Input Action을 더블 클릭하면 창이 나온다.
여기서 이제 앞에서 했던 입력 시스템을 관리 해줄 것이다.
Action Map을 하나 추가하면 New action항목이 Actions 안에 하나 생긴다
오른쪽 마우스를 눌러서 이름을 변경 해줄 수 있다.
WSAD를 누르면 이동하는 Input 값을 넣어 줄거기 떄문에
Action Type을 기본값(Button)이 아닌 Value를 선택해주고
Control Type을 Vector2 로 선택해준다.
그리고 Actions에서 + 아이콘을 누르거나 오른쪽마우스를 클릭하면 Add 속성들이 나온다
방향키로 이동하는 Input을 만들어 줄 것이기 때문에 Up\Down\Left\Right Composite를 선택해주면
오른쪽 그림과 같이 아무것도 들어가지 않은 4가지의 방향이 생성되는것을 볼 수 있다.
그리고 각각의 방향안에 Path를 등록해주면 되는데
Listen 버튼을 누르고 넣어주고싶은 키의 값을 입력하면 쉽게 찾을 수 있다.
키 값을 다 지정해주었으면 Save Asset 버튼을 눌러서 저장해준다.
만들어둔 PlayerInputAction으로 들어가서
Generate C# Class 버튼을 체크하고 Apply 해주면 C# 스크립트를 하나 만들어준다
이제 만들어진 InputAction을 사용하기 위해서
Player 안에있는 스크립트에서 PlayerInputAction 객체를 하나 생성해준다.
private PlayerInputAction playerInputAction;
private void Awake()
{
playerInputAction =new PlayerInputAction();
}
그리고 사용할 Action Maps을 활성화 해주어야한다.
playerInputAction.Player.Enable();
Player 캐릭터에게 적용할 Action Maps이기 때문에 Player를 활성화 해주어야한다.
Vector2 inputVector = playerInputAction.Player.Move.ReadValue<Vector2>();
ReadValue<'Control Type'>() 으로 입력한 키값을 받아서 넣어 줄 수 있다.
public class GameInput : MonoBehaviour
{
private PlayerInputAction playerInputAction;
private void Awake()
{
playerInputAction =new PlayerInputAction();
playerInputAction.Player.Enable();
}
public Vector2 GetMovementVectorNormalized()
{
Vector2 inputVector = playerInputAction.Player.Move.ReadValue<Vector2>();
inputVector = inputVector.normalized;
return inputVector;
}
}
컴포넌트를 직접 넣어서 사용도 가능하다