3-1. 키보드와 마우스로 입력 받아 이동시키기

2023. 6. 30. 17:21C# & Unity 공부

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

public class LifeCycle : MonoBehaviour
{
    void Update()
    {
        //Input은 게임 내 모든 입력을 관리하는 클래스 (기본으로 제공)
        
        if (Input.anyKeyDown)    //anyKeyDown : 아무 키를 최로 받을 때 true 값을 출력함 (Bool 형태 반환, 변수)
            Debug.Log("플레이어가 아무 키를 눌렀습니다.");

        /*if (Input.anyKey)        //anyKey : 아무 키를 누르고 있을 떄 true 값을 출력함 (Bool 형태 반환, 변수)
            Debug.Log("플레이어가 아무 키를 누르고 있습니다.");*/

        /*입력 방식은 총 3가지 형태가 있다.
         Down:눌렀을 때
         Stay:누르고 있을 때
         Up: 누르고 뗀 경우
         */

        /*
        if (Input.GetKeyDown(KeyCode.Return))
            Debug.Log("아이템을 구입하였습니다.");
        
        if (Input.GetKey(KeyCode.LeftArrow))
            Debug.Log("왼쪽으로 이동 중");
        
        if (Input.GetKeyUp(KeyCode.RightArrow))
            Debug.Log("오른쪽 이동을 멈추었습니다.");
        */

        /*
         GetKey:키보드 입력을 받으면 true를 반환하는 함수
         KeyCode:클래스의 한 종류로, 그안에 키보드의 키를 나타내는 변수를 가지고 있음
         Return : Enter를 나타냄
         LeftArrow : 왼쪽 키를 나타냄
         RightArrow : 오른쪽 키를 나타냄
         Escape : Esc를 나타냄
         등등
         */

        /*
        if (Input.GetMouseButtonDown(0))
            Debug.Log("미사일 발사");

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

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

        /*
         GetMouseButton : 마우스 입력을 받으면 true를 반환하는 함수
         0은 좌클릭, 1은 우클릭을 나타냄
         */

        /*
         버튼 방식 : 조금 더 세밀하고 유동적인 방식. Unity에서 공식적으로 지정해준 키를 가지고 사용하는 것.
         Edit->Project Settings->Input Manager->Axes (Unity)
         Horizontal => 횡이동 (A,D키)
         Vertical => 종이동 (W,S키)
         등등
         jump 키를 넣어보자
         space바로 되어있다
         */

        /*
        if (Input.GetButtonDown("Jump"))
            Debug.Log("점프");

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

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

        /*
        GetButton : 버튼 방식을 사용한, Unity에서 지정된 키보드 입력을 받으면 true를 출력하는 함수
        Input Manager에 있는 Size를 늘리면 내가 원하는 키를 추가할 수 있다
        */

        //Fire1을 가지고 해보자

        /*
        if (Input.GetButtonDown("Fire1"))
            Debug.Log("발사");

        if (Input.GetButton("Fire1"))
            Debug.Log("레이저 모으는 중...");

        if (Input.GetButtonUp("Fire1"))
            Debug.Log("레이저!!");
        */

        /*
        if (Input.GetButton("Horizontal"))
        {
            Debug.Log("횡 이동 중..."+ Input.GetAxis("Horizontal"));
            //Debug.Log("횡 이동 중..." + Input.GetAxisRaw("Horizontal"));
        }

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

        /*
        Horizontal과 Vertical은 값이 있다.
        GetAxis() : 수평 수직 버튼 입력을 받으면 float형을 반환. 가중치가 있음(살짝 누르고 길게 누르고 차이가 있음)
        GetAxisRaw() : GetAxis에서 가중치가 사라진 것. 오로지 1과 -1 혹은 0을 반환
        */
    }
}