유니티 일기

키보드 마우스 입력 받기

mky 2025. 8. 24. 15:57

골드메탈님의 유튜브 강의 내용을 정리해보았다.

using UnityEngine;

public class LifeCycle : MonoBehaviour
{

    void Start() // Transform: 오브젝트 형태에 대한 기본 컴포넌트
    {

    }
    void Update() // 프레임(로직), 환경에 따라 실행 주기가 떨어질 수 있음
    {
        Vector3 vec = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0);
        transform.Translate(vec); // Translate: 벡터 값을 현재 위치에 더하는 함수

        // Input: 게임 내 입력을 관리하는 클래스, 
        // 입력 방식의 종류 : Down, Stay, Up
        // 각 입력 함수는 3가지 행동으로 구분

        //anyKeyDown: 아무 키나 눌렸을 때 true
        if (Input.anyKeyDown) 
            Debug.Log("플레이어가 아무 키를 눌렀습니다.");

        // GetKey: 키보드 버튼 입력을 받으면 true
        /*if (Input.GetKeyDown(KeyCode.Return)) // Return: Enter 키, Escape: esc 키
            Debug.Log("아이템을 구매하였습니다.");

        if (Input.GetKey(KeyCode.LeftArrow)) // Arrow: 방향키 
            Debug.Log("왼쪽으로 이동 중");

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

        if (Input.GetMouseButtonDown(0)) // 0: 좌클릭, 1: 우클릭, 2: 휠클릭
            Debug.Log("미사일 발사!");

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

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

        //Input Manager에서 버튼 설정 가능
        if (Input.GetButtonDown("Jump")) 
            Debug.Log("점프!");

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

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

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

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

}