Unity Input Manager


Unity ‘Project Setting’의 ‘Input Manager’ (Unity ‘Project Setting’ – ‘Input Manager’)

Input Manager는 Unity에서 입력을 관리하고 설정하는 중요한 도구입니다. 이를 통해 게임 개발자는 다양한 입력 장치(키보드, 마우스, 게임패드 등)에서 입력을 받아 게임 내에서 이를 처리할 수 있습니다. 이 문서에서는 Input Manager의 소개와 상세한 설정 방법, 함수 목록, 그리고 다양한 예제를 통해 이를 설명합니다.

Input Manager 소개 (Introduction to Input Manager)

Input Manager는 Unity의 Project Settings에서 접근할 수 있는 구성 요소로, 다양한 입력 축(axis)과 버튼을 정의하고 관리할 수 있습니다. 이를 통해 게임 내에서 다양한 입력 방식을 처리할 수 있습니다. 기본적으로 키보드, 마우스, 조이스틱 등 다양한 입력 장치에서 입력을 처리할 수 있습니다.

Input Manager 설정 방법 (Configuring Input Manager)

  1. Input Manager 접근 방법 (Accessing Input Manager) Unity 에디터에서 메뉴 바의 Edit -> Project Settings -> Input Manager를 선택합니다. 여기서 다양한 입력 축과 버튼을 설정할 수 있는 창이 나타납니다.
  2. 축(axis) 설정 (Configuring Axes) Input Manager에서 축(axis)은 연속적인 입력 값을 나타내며, 예를 들어 조이스틱의 좌우 이동이나 마우스의 이동 등을 설정할 수 있습니다. 기본적으로 Unity는 Horizontal과 Vertical 축을 제공하며, 이는 키보드의 화살표 키나 조이스틱의 움직임을 처리합니다.
   Name: Horizontal
   Descriptive Name: 
   Descriptive Negative Name: 
   Negative Button: left
   Positive Button: right
   Alt Negative Button: a
   Alt Positive Button: d
   Gravity: 3
   Dead: 0.001
   Sensitivity: 3
   Type: Keyboard or Mouse
   Axis: X axis
   Joy Num: Get Motion from all Joysticks
  1. 버튼 설정 (Configuring Buttons) Input Manager에서 버튼은 이진 입력 값을 나타내며, 예를 들어 점프, 발사 등의 동작을 설정할 수 있습니다.
   Name: Jump
   Descriptive Name: 
   Descriptive Negative Name: 
   Negative Button: 
   Positive Button: space
   Alt Negative Button: 
   Alt Positive Button: 
   Gravity: 1000
   Dead: 0.001
   Sensitivity: 1000
   Type: Key or Mouse Button

함수 목록 (Function List)

  1. GetAxis
  • 설명: 지정된 축의 값을 반환합니다.
  • 사용법: float value = Input.GetAxis("Horizontal");
  • 반환값: float (축의 현재 값)
  1. GetAxisRaw
  • 설명: 지정된 축의 원시 값을 반환합니다.
  • 사용법: float value = Input.GetAxisRaw("Vertical");
  • 반환값: float (축의 원시 값)
  1. GetButton
  • 설명: 지정된 버튼이 눌렸는지 여부를 반환합니다.
  • 사용법: bool isPressed = Input.GetButton("Jump");
  • 반환값: bool (버튼이 눌렸는지 여부)
  1. GetButtonDown
  • 설명: 지정된 버튼이 눌리기 시작했는지 여부를 반환합니다.
  • 사용법: bool isPressed = Input.GetButtonDown("Fire1");
  • 반환값: bool (버튼이 눌리기 시작했는지 여부)
  1. GetButtonUp
  • 설명: 지정된 버튼이 떼어졌는지 여부를 반환합니다.
  • 사용법: bool isReleased = Input.GetButtonUp("Fire2");
  • 반환값: bool (버튼이 떼어졌는지 여부)
  1. GetKey
  • 설명: 지정된 키가 눌렸는지 여부를 반환합니다.
  • 사용법: bool isPressed = Input.GetKey(KeyCode.Space);
  • 반환값: bool (키가 눌렸는지 여부)
  1. GetKeyDown
  • 설명: 지정된 키가 눌리기 시작했는지 여부를 반환합니다.
  • 사용법: bool isPressed = Input.GetKeyDown(KeyCode.A);
  • 반환값: bool (키가 눌리기 시작했는지 여부)
  1. GetKeyUp
  • 설명: 지정된 키가 떼어졌는지 여부를 반환합니다.
  • 사용법: bool isReleased = Input.GetKeyUp(KeyCode.D);
  • 반환값: bool (키가 떼어졌는지 여부)

다양한 예제 (Various Examples)

  1. 캐릭터 이동 (Character Movement) 캐릭터를 이동시키기 위해 Horizontal과 Vertical 축을 설정합니다.
public class PlayerMovement : MonoBehaviour{
   public float speed = 5.0f;

   void Update(){
      float moveHorizontal = Input.GetAxis("Horizontal");
      float moveVertical = Input.GetAxis("Vertical");

      Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
      transform.Translate(movement * speed * Time.deltaTime, Space.World);
   }
}
  1. 캐릭터 점프 (Character Jump) 점프 버튼을 설정하여 캐릭터가 점프할 수 있도록 합니다.
public class PlayerJump : MonoBehaviour{
   public float jumpForce = 5.0f;
   private Rigidbody rb;

   void Start(){
      rb = GetComponent<Rigidbody>();
   }

   void Update(){
      if (Input.GetButtonDown("Jump") && Mathf.Abs(rb.velocity.y) < 0.001f) {
            rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
      }
   }
}
  1. 마우스 이동을 통한 카메라 회전 (Camera Rotation with Mouse Movement) 마우스 이동을 이용해 카메라를 회전시킵니다.
public class CameraController : MonoBehaviour{
   public float sensitivity = 5.0f;
   public Transform playerBody;

   private float xRotation = 0.0f;

   void Update(){
       float mouseX = Input.GetAxis("Mouse X") * sensitivity;
       float mouseY = Input.GetAxis("Mouse Y") * sensitivity;

       xRotation -= mouseY;
       xRotation = Mathf.Clamp(xRotation, -90f, 90f);

       transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
       playerBody.Rotate(Vector3.up * mouseX);
   }
}

Input Manager 활용 팁 (Tips for Using Input Manager)

  • 축 복제 (Duplicating Axes) 동일한 설정을 가진 축을 여러 개 만들 때는 하나의 축을 복제하여 이름만 변경하면 편리합니다.
  • 커스텀 입력 (Custom Inputs) 게임의 특성에 맞춰 커스텀 입력을 추가하여 다양한 입력 장치를 지원할 수 있습니다.
  • 입력 테스트 (Testing Inputs) 입력 설정 후, Unity 에디터의 Play 모드에서 입력이 제대로 동작하는지 테스트하는 것이 중요합니다.

마무리 (Conclusion)

Unit마무리 (Conclusion)y의 Input Manager는 다양한 입력 장치를 관리하고 게임 내에서 이를 활용할 수 있는 강력한 도구입니다. 이 가이드를 통해 기본적인 설정 방법과 예제, 그리고 주요 함수 목록을 익히고, 이를 바탕으로 자신만의 커스텀 입력을 설정하여 게임을 더욱 풍부하게 만들 수 있습니다. 다양한 입력을 효과적으로 관리하고 처리함으로써 게임 개발의 효율성을 높일 수 있습니다.


Leave a Reply

Your email address will not be published. Required fields are marked *