Unity Input Handling


유니티 입력 처리 (Unity Input Handling)

소개(Introduction):
유니티의 입력 처리 시스템은 게임에서 사용자의 입력을 감지하고, 이를 통해 게임 오브젝트와 상호작용할 수 있도록 하는 기능을 제공합니다. 키보드, 마우스, 게임패드, 터치 스크린 등 다양한 입력 장치를 지원하며, 이를 통해 캐릭터 이동, 공격, UI 조작 등의 다양한 게임 기능을 구현할 수 있습니다.

기본 입력 처리(Basic Input Handling):
유니티의 기본 입력 처리는 Input 클래스를 통해 이루어집니다. 이 클래스는 키보드, 마우스, 게임패드 등의 입력을 감지할 수 있는 다양한 메서드를 제공합니다.

using UnityEngine;

public class BasicInputHandling : MonoBehaviour {
    void Update() {
        if (Input.GetKeyDown(KeyCode.Space)) {
            Debug.Log("스페이스바가 눌렸습니다.");
        }

        if (Input.GetMouseButtonDown(0)) {
            Debug.Log("마우스 왼쪽 버튼이 눌렸습니다.");
        }

        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(horizontal, 0, vertical);
        transform.Translate(movement * Time.deltaTime);
    }
}

키 입력(Key Input):
키보드 입력을 처리하려면 Input.GetKey, Input.GetKeyDown, Input.GetKeyUp 메서드를 사용합니다. GetKey는 키가 눌려있는 동안 계속해서 true를 반환하고, GetKeyDown은 키가 눌리는 순간에 한 번, GetKeyUp은 키가 떼어지는 순간에 한 번 true를 반환합니다.

using UnityEngine;

public class KeyInputExample : MonoBehaviour {
    void Update() {
        if (Input.GetKey(KeyCode.W)) {
            Debug.Log("W 키가 눌려있습니다.");
        }

        if (Input.GetKeyDown(KeyCode.W)) {
            Debug.Log("W 키가 눌렸습니다.");
        }

        if (Input.GetKeyUp(KeyCode.W)) {
            Debug.Log("W 키가 떼어졌습니다.");
        }
    }
}

마우스 입력(Mouse Input):
마우스 입력은 Input.GetMouseButton, Input.GetMouseButtonDown, Input.GetMouseButtonUp 메서드를 통해 처리합니다. 또한, 마우스 커서의 위치를 Input.mousePosition을 통해 얻을 수 있습니다.

using UnityEngine;

public class MouseInputExample : MonoBehaviour {
    void Update() {
        if (Input.GetMouseButton(0)) {
            Debug.Log("마우스 왼쪽 버튼이 눌려있습니다.");
        }

        if (Input.GetMouseButtonDown(0)) {
            Debug.Log("마우스 왼쪽 버튼이 눌렸습니다.");
        }

        if (Input.GetMouseButtonUp(0)) {
            Debug.Log("마우스 왼쪽 버튼이 떼어졌습니다.");
        }

        Vector3 mousePosition = Input.mousePosition;
        Debug.Log("마우스 위치: " + mousePosition);
    }
}

축 입력(Axis Input):
축 입력은 Input.GetAxisInput.GetAxisRaw 메서드를 통해 처리합니다. GetAxis는 -1에서 1 사이의 값을 반환하여 부드러운 움직임을 제공하고, GetAxisRaw는 -1, 0, 1의 정수 값을 반환하여 즉각적인 반응을 제공합니다.

using UnityEngine;

public class AxisInputExample : MonoBehaviour {
    void Update() {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector3 movement = new Vector3(horizontal, 0, vertical);
        transform.Translate(movement * Time.deltaTime);

        Debug.Log("수평 입력: " + horizontal);
        Debug.Log("수직 입력: " + vertical);
    }
}

터치 입력(Touch Input):
모바일 장치에서 터치 입력을 처리하기 위해 Input.touchCountInput.GetTouch 메서드를 사용합니다. touchCount는 현재 화면을 터치하고 있는 손가락의 수를 반환하며, GetTouch는 특정 터치에 대한 정보를 제공합니다.

using UnityEngine;

public class TouchInputExample : MonoBehaviour {
    void Update() {
        if (Input.touchCount > 0) {
            Touch touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Began) {
                Debug.Log("터치 시작");
            } else if (touch.phase == TouchPhase.Moved) {
                Debug.Log("터치 이동 중");
            } else if (touch.phase == TouchPhase.Ended) {
                Debug.Log("터치 종료");
            }
        }
    }
}

새 입력 시스템(New Input System):
유니티는 기존 입력 시스템 외에도 새로운 입력 시스템을 제공합니다. 새로운 입력 시스템은 더 많은 기능과 유연성을 제공하며, 패키지 매니저를 통해 설치할 수 있습니다. 설치 후, Input Actions 에셋을 생성하여 다양한 입력을 처리할 수 있습니다.

using UnityEngine;
using UnityEngine.InputSystem;

public class NewInputSystemExample : MonoBehaviour {
    public InputAction moveAction;

    void OnEnable() {
        moveAction.Enable();
    }

    void OnDisable() {
        moveAction.Disable();
    }

    void Update() {
        Vector2 movement = moveAction.ReadValue<Vector2>();
        Debug.Log("움직임: " + movement);
    }
}

유니티의 입력 처리 시스템은 다양한 입력 장치를 지원하며, 게임 플레이어와의 상호작용을 쉽게 구현할 수 있도록 도와줍니다. 이를 통해 키보드, 마우스, 게임패드, 터치 스크린 등 다양한 장치로 게임을 제어하고, 플레이어에게 보다 직관적이고 반응성 있는 경험을 제공할 수 있습니다.


Leave a Reply

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