Game AI & Unity/Unity

[Unity] 6. 키보드, 마우스 입력 (코드로 직접)

bay07 2024. 3. 23. 08:33

출처 : 골드메탈님 유니티 강의 (아래 영상첨부)


1. 키보드 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    void Update() {

        // 키보드 
        if (Input.anyKeyDown)
            Debug.Log("플레이어가 아무 키를 눌렀습니다");

        if (Input.anyKey)
            Debug.Log("플레이어가 아무 키를 누르고 있습니다");
        // 여기서 Return은 그냥 엔터 누른 값이다

        if(Input.GetKeyDown(KeyCode.Return))
            Debug.Log("아이템을 구입하였습니다");

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

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

    }

}

 


 

2. 마우스

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    void Update() {

        // 마우스
        if (Input.anyKeyDown)
            Debug.Log("플레이어가 아무 키를 눌렀습니다");

        // 마우스에는 0이랑 1이 있다
        //  0은 마우스 왼쪽 버튼, 1은 마우스 오르쪽 버튼이다
        if (Input.GetMouseButtonDown(0))
            Debug.Log("미사일 발사!");

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

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

    }

}