본문 바로가기
Programming/Unity

[Unity] 7. 키보드 및 마우스 입력

by 가가가가가가 2022. 6. 28.
반응형
  • Input : 게임 내 입력을 관리하는 클래스
    - 마우스, 키보드 등

 

  • anyKeyDown : 아무 입력을 최초로 받을 때 true
if (Input.anyKeyDown)
{
    Debug.Log("플레이어가 아무 키를 눌렀습니다.");
}

 

  • anyKey : 아무 입력을 받으면 true
if (Input.anyKey)
{
    Debug.Log("플레이어가 아무 키를 누르고 있습니다.");
}

 

  • (anyKeyUp은 따로 없음)

 

  • 각 입력 함수는 3가지 행동으로 구분
    - Down / Stay / Up

 

  • GetKey : 키보드 버튼 입력을 받으면 true
    - Input.GetKeyDown() / Input.GetKey() / Input.GetKeyUp()
    - Enter : KeyCode.Return (KeypadEnter은 오른쪽 키패드의 엔터키)
    - ESC : KeyCode.Escape
if (Input.GetKeyDown(KeyCode.Return))
{
    Debug.Log("아이템을 구입하였습니다.");
}

if (Input.GetKey(KeyCode.LeftArrow))
{
    Debug.Log("왼쪽으로 이동 중");
}

if (Input.GetKeyUp(KeyCode.RightArrow))
{
    Debug.Log("오른쪽 이동을 멈추었습니다.");
}

 

  • GetMouse : 마우스 버튼 입력을 받으면 true
    - Input.GetMouseButtonDown() / Input.GetMouseButton() / Input.GetMouseButtonUp()
    - 0 : 왼쪽 마우스
    - 1 : 오른쪽 마우스
if (Input.GetMouseButtonDown(0))
{
    Debug.Log("미사일 발사!");
}

if (Input.GetMouseButton(0))
{
    Debug.Log("미사일 모으는 중...");
}

if (Input.GetMouseButtonUp(0))
{
    Debug.Log("슈퍼 미사일 발사!!");
}

 

  • Input Manager에서 Button 설정 가능
    - Edit > Project Settings > Input Manager

 

  • GetButton : Input 버튼 입력을 받으면 true
    - parameter 안에 들어가는 문자열은 Input Manager의 Name(대소문자 구별 주의)

 

if (Input.GetButtonDown("Jump"))
{
    Debug.Log("점프!");
}

if (Input.GetButton("Jump"))
{
    Debug.Log("점프 모으는 중...");
}

if (Input.GetButtonUp("Jump"))
{
    Debug.Log("슈퍼 점프!!");
}

 

  • Button 새로 추가 및 기존 변경 가능
    - 추가 : Axes Size + 1 > 추가된 항목의 이름 변경

if (Input.GetButtonDown("Superfire"))
{
    Debug.Log("필살기!");
}

 

  • GetAxis : 수평, 수직 버튼 입력을 받으면 float값을 반환
if (Input.GetButton("Horizontal"))
{
    Debug.Log("횡 이동 중..." + Input.GetAxis("Horizontal"));
}

 

  • 오브젝트는 변수 transform을 항상 가지고 있음
if (Input.GetButton("Horizontal"))
{
    Debug.Log("횡 이동 중..." + Input.GetAxisRaw("Horizontal"));
}

if (Input.GetButton("Vertical"))
{
    Debug.Log("종 이동 중..." + Input.GetAxisRaw("Vertical"));
}

 


참고

 

 

반응형

'Programming > Unity' 카테고리의 다른 글

[Unity] 9. 목표 지점 이동  (0) 2022.07.10
[Unity] 8. 오브젝트 이동  (0) 2022.07.10
[Unity] 프로젝트명 변경  (0) 2022.06.28
[Unity] 6. 게임 오브젝트의 흐름  (1) 2022.06.28
[Unity] 5. C# 프로그래밍 기초  (1) 2022.06.26

댓글