What's Life Studio왓츠라이프 스튜디오

· Unity3D / Basics

유니티에서의 키 입력 받기 - Getkey

GetKey를 이용한 키 입력

유니티에서의 키 입력

유니티에서 Getkey메소드를 활용한 키 입력을 알아봅니다.

유니티3D 에디터에서

스크립트 작성

프로젝트창에서 GetInput스크립트를 생성합니다.

GetInput 스크립트를 열어서 다음과 같이 작성합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 void Update()
    {
        if (Input.GetKey("up"))
            print("up arrow key is held down");

        if (Input.GetKeyDown("down"))
            print("down arrow key is down");

        if (Input.GetKeyUp("right"))
            print("right arrow key is up");

        if(Input.GetKey(KeyCode.LeftArrow))
            print("left arrow key is held down");
    }
  • 기본적인 구조는 GetMouseButton, GetMouseButtonDown, GetMouseButtonUp의 원리와 같습니다.
  • GetKey 메소드와 " "안에 문자열 값으로 키 값을 제공하면 해당 키를 누를 때 입력 값을 가져올 수 있습니다.
  • GetKey 메소드에 Keycode.키값 을 이용해서 키값을 가져오는 방법도 있습니다.
    (Keycode의 키값은 https://docs.unity3d.com/kr/530/ScriptReference/KeyCode.html 에서 볼 수 있습니다)

테스트

MainCamera오브젝트에 GetInput스크립트를 인스턴스화 시켜서 테스트를 해봅니다.


<01. GetInput 스크립트 인스턴스화>

결과물


<02. Console창 확인>